Filtering users with the pickSPUser widget in SharePoint Forms
Over the last few months I have been using a jQuery plugin by Paul Tavares called SPWidgets. In particular, the pickSPUser and SPControlUpload components have both been quite useful in some SharePoint 2010 forms that I have been developing.
This widget works in a similar way to standard SharePoint people picker control, in that it can sometimes return unwanted suggestions. In my case, we had people from a couple of different domains showing up which weren’t applicable in the context of the form. The pickSPUser component provides a helpful option that enables you to specify a function to filter the suggested people.
The code below initialises the people picker control and filters the user suggestions to the specified AD domain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
var domain = "DOMAINNAME"; var pickSPUserOptions = { allowMultiples: false, inputPlaceholder: 'Enter a person', filterSuggestions: function(data) { var newUserList = []; for(var i = 0; i < data.length; i++) { var userAccountName = data[i].accountName.toUpperCase(); var patt = new RegExp("^(" + domain + "\\\\)"); var validDomainUser = patt.test(userAccountName); if(validDomainUser) { newUserList.push(data[i]); } } return newUserList; } } $(element).pickSPUser(pickSPUserOptions); |
Now, if all goes to plan when a user begins to enter a person’s name in the input box it should only provide relevant suggestions.
Karar Raheem
Brilliant. Is there a way to populate more fields like email, accountName..etc?
Thanks a lot