People Picker Field Resolve In SharePoint 2013 (ClientPeoplePicker) Using REST APi And jQuery

This blog will help you to resolve the Custom People Picker field to fetch the user details in the text box, using REST API and jQuery.

The good news is that you can implement your own people picker, using the “_api/SP.UI.ApplicationPages.

ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser” endpoint along with jQuery-UI.

It does not require to load any Dependent JS files and makes it very easy to implement multiple people pickers. 
  1. <script>  
  2. $(document).ready(function () {  
  3.     $("#PeoplePickerinptbox").autocomplete({  
  4.         source: search,  
  5.         minLength: 2  
  6.     });  
  7.     
  8. });  
  9.   
  10. function search(request,response) {  
  11.     var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));  
  12.     var hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));  
  13.    
  14.     var sourceURL = appweburl + "/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser";  
  15.     var principalType = this.element[0].getAttribute('principalType');  
  16.     $.ajax(   
  17.     {  
  18.         'url':sourceURL,  
  19.         'method':'POST',  
  20.         'data':JSON.stringify({  
  21.             'queryParams':{  
  22.                 '__metadata':{  
  23.                     'type':'SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters'  
  24.                 },  
  25.                 'AllowEmailAddresses':true,  
  26.                 'AllowMultipleEntities':false,  
  27.                 'AllUrlZones':false,  
  28.                 'MaximumEntitySuggestions':50,  
  29.                 'PrincipalSource':15,  
  30.                 'PrincipalType': principalType,  
  31.                 'QueryString':request.term  
  32.                 'Required':false  
  33.                  
  34.             }  
  35.         }),  
  36.         'headers':{  
  37.             'accept':'application/json;odata=verbose',  
  38.             'content-type':'application/json;odata=verbose',  
  39.             'X-RequestDigest':requestDigest  
  40.         },  
  41.         'success':function (data) {   
  42.             var d = data;  
  43.             var results = JSON.parse(data.d.ClientPeoplePickerSearchUser);  
  44.             if (results.length > 0) {  
  45.                 response($.map(results, function (item) {  
  46.                     return {label:item.DisplayText,value:item.DisplayText}  
  47.                 }));  
  48.             }              
  49.         },  
  50.         'error':function (err) {   
  51.             alert(JSON.stringify(err));   
  52.         }  
  53.     }  
  54.   );  
  55.    
  56.    
  57. }  
  58.   
  59. </script>  
Was my blog helpful?

If so, please let us know at the bottom of this page. If not, let us know what was confusing or missing and I’ll use your feedback to double-check the facts, add info and update this blog.