none
CSOM/JSOM Utility.SearchPrincipals operator (StartsWith/Contains) RRS feed

  • Question

  • hi all,

    We are using javascript object model to retrieve users using the SearchPrincipals method.

    What we noticed is that depending on the source (PrincipalSource argument), the operator applied in search is different.

    More specifically, when PrincipalSource = Windows, then the search works with "Contains", while when source = "UserInfoList" the operator is StartsWith.

    This cause users who exist in user information list, to be returned with ID=-1 , even though they have been added to the site collection. Does anybody know any way to control the operator applied in SearchPrincipals JSOM/CSOM method?

    Thanks,

    Spyros

    Wednesday, May 3, 2017 5:20 PM

Answers

  • Hi Spyros,

    Please try to set PrincipleSource to All.

    Or we can use CAML query and JSOM to get the users.

    var camlQuery = new SP.CamlQuery();
    
    camlQuery.set_viewXml('<View Scope=\'RecursiveAll\'><Query><Where><Contains><FieldRef Name=\'Name\'/>' +'<Value Type=\'Text\'>' + username + '</Value></Contains>' +'</Where></Query><RowLimit>10</RowLimit></View>'); 
    

    https://social.technet.microsoft.com/wiki/contents/articles/15061.how-to-get-user-information-using-javascript-clientobject-model-or-ecma-in-share-point-2010.aspx

    Or use SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser method.

    https://msdn.microsoft.com/en-us/library/office/jj850862.aspx?f=255&MSPPError=-2147217396

    http://sharepointfieldnotes.blogspot.sg/2014/06/sharepoint-2013-clientpeoplepicker.html

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, May 4, 2017 7:27 AM
    Moderator
  • Hi Dennis,

    Sorry for the delayed response, I just had time to try out the clientPeoplePickerSearchUser rest api method.

    It seems that the operator used to search to both Windows and UserInfoList sources is the same when using this function, so the same results are retrieved for the same queries, which is what we initially wanted.

    For anyone who needs a sample code, I have used the following snippet in a SharePoint web part page (based on the article you sent me), in order to compare the results retrieved from both sources:

    <link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    
    Search user from Windows: <input principalSource=2 id=txtPeoplePickerWindows name=txtPeoplePickerWindows />
    Search user from User Info List: <input principalSource=1 id=txtPeoplePickerUserInfoList name=txtPeoplePickerUserInfoList />
    
    <script>
    
    //PrincipalSource: What sources you wish to search. Choices are  All - 15 , Membership Provider - 4 , RoleProvider - 8, UserInfoList - 1  or Windows - 2. These values can be combined.
    //PrincipalType: Controls the type of entities that are returned in the results. Choices are All - 15, Distribution List - 2 , Security Groups - 4,  SharePoint Groups – 8, User – 1. These values can be combined.
    
    $(document).ready(function () {
        $("#txtPeoplePickerWindows").autocomplete({
            source: search,
            minLength: 2
        });
        $("#txtPeoplePickerUserInfoList").autocomplete({
            source: search,
            minLength: 2
        });
    });
    
    
    function search(request,response) {
     
        var restSource = "https://bpcgr.sharepoint.com/sites/Dev/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser";
        var principalSource = this.element[0].getAttribute('principalSource');
        $.ajax( 
        {
            'url':restSource,
            'method':'POST',
            'data':JSON.stringify({
                'queryParams':{
                    '__metadata':{
                        'type':'SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters'
                    },
                    'AllowEmailAddresses':true,
                    'AllowMultipleEntities':false,
                    'AllUrlZones':false,
                    'MaximumEntitySuggestions':50,
                    'PrincipalSource':principalSource,
                    'PrincipalType': 1, //user
                    'QueryString':request.term
                    //'Required':false,
                    //'SharePointGroupID':null,
                    //'UrlZone':null,
                    //'UrlZoneSpecified':false,
                    //'Web':null,
                    //'WebApplicationID':null
                }
            }),
            'headers':{
                'accept':'application/json;odata=verbose',
                'content-type':'application/json;odata=verbose',
                "X-RequestDigest": $("#__REQUESTDIGEST").val()            
            },
            'success':function (data) { 
                var d = data;
                var results = JSON.parse(data.d.ClientPeoplePickerSearchUser);
                if (results.length > 0) {
                    response($.map(results, function (item) {
                        return {label:item.DisplayText,value:item.DisplayText}
                    }));
                }            
            },
            'error':function (err) { 
                alert(JSON.stringify(err)); 
            }
        }
      );
     
     
    }
    </script>


    Thanks again,

    Spyros


    • Marked as answer by spyrosnb Sunday, May 28, 2017 2:00 PM
    • Edited by spyrosnb Sunday, May 28, 2017 2:00 PM highlighted the method
    Sunday, May 28, 2017 1:59 PM

All replies

  • Hi Spyros,

    Please try to set PrincipleSource to All.

    Or we can use CAML query and JSOM to get the users.

    var camlQuery = new SP.CamlQuery();
    
    camlQuery.set_viewXml('<View Scope=\'RecursiveAll\'><Query><Where><Contains><FieldRef Name=\'Name\'/>' +'<Value Type=\'Text\'>' + username + '</Value></Contains>' +'</Where></Query><RowLimit>10</RowLimit></View>'); 
    

    https://social.technet.microsoft.com/wiki/contents/articles/15061.how-to-get-user-information-using-javascript-clientobject-model-or-ecma-in-share-point-2010.aspx

    Or use SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser method.

    https://msdn.microsoft.com/en-us/library/office/jj850862.aspx?f=255&MSPPError=-2147217396

    http://sharepointfieldnotes.blogspot.sg/2014/06/sharepoint-2013-clientpeoplepicker.html

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, May 4, 2017 7:27 AM
    Moderator
  • hi Dennis,

    Thanks for the reply. Unfortunately querying the User Information List would not be acceptable, because the average user doesn't have permissions to this list (and we are not currently permitted to change the access rights in the specific site collection). 

    We will check the clientPeoplePickerSearchUSer method and revert.

    Regards

    Thursday, May 4, 2017 1:14 PM
  • Hi,

    Any update?

    Did you solve this issue?

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, May 16, 2017 2:35 AM
    Moderator
  • Hi Dennis,

    Sorry for the delayed response, I just had time to try out the clientPeoplePickerSearchUser rest api method.

    It seems that the operator used to search to both Windows and UserInfoList sources is the same when using this function, so the same results are retrieved for the same queries, which is what we initially wanted.

    For anyone who needs a sample code, I have used the following snippet in a SharePoint web part page (based on the article you sent me), in order to compare the results retrieved from both sources:

    <link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    
    Search user from Windows: <input principalSource=2 id=txtPeoplePickerWindows name=txtPeoplePickerWindows />
    Search user from User Info List: <input principalSource=1 id=txtPeoplePickerUserInfoList name=txtPeoplePickerUserInfoList />
    
    <script>
    
    //PrincipalSource: What sources you wish to search. Choices are  All - 15 , Membership Provider - 4 , RoleProvider - 8, UserInfoList - 1  or Windows - 2. These values can be combined.
    //PrincipalType: Controls the type of entities that are returned in the results. Choices are All - 15, Distribution List - 2 , Security Groups - 4,  SharePoint Groups – 8, User – 1. These values can be combined.
    
    $(document).ready(function () {
        $("#txtPeoplePickerWindows").autocomplete({
            source: search,
            minLength: 2
        });
        $("#txtPeoplePickerUserInfoList").autocomplete({
            source: search,
            minLength: 2
        });
    });
    
    
    function search(request,response) {
     
        var restSource = "https://bpcgr.sharepoint.com/sites/Dev/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser";
        var principalSource = this.element[0].getAttribute('principalSource');
        $.ajax( 
        {
            'url':restSource,
            'method':'POST',
            'data':JSON.stringify({
                'queryParams':{
                    '__metadata':{
                        'type':'SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters'
                    },
                    'AllowEmailAddresses':true,
                    'AllowMultipleEntities':false,
                    'AllUrlZones':false,
                    'MaximumEntitySuggestions':50,
                    'PrincipalSource':principalSource,
                    'PrincipalType': 1, //user
                    'QueryString':request.term
                    //'Required':false,
                    //'SharePointGroupID':null,
                    //'UrlZone':null,
                    //'UrlZoneSpecified':false,
                    //'Web':null,
                    //'WebApplicationID':null
                }
            }),
            'headers':{
                'accept':'application/json;odata=verbose',
                'content-type':'application/json;odata=verbose',
                "X-RequestDigest": $("#__REQUESTDIGEST").val()            
            },
            'success':function (data) { 
                var d = data;
                var results = JSON.parse(data.d.ClientPeoplePickerSearchUser);
                if (results.length > 0) {
                    response($.map(results, function (item) {
                        return {label:item.DisplayText,value:item.DisplayText}
                    }));
                }            
            },
            'error':function (err) { 
                alert(JSON.stringify(err)); 
            }
        }
      );
     
     
    }
    </script>


    Thanks again,

    Spyros


    • Marked as answer by spyrosnb Sunday, May 28, 2017 2:00 PM
    • Edited by spyrosnb Sunday, May 28, 2017 2:00 PM highlighted the method
    Sunday, May 28, 2017 1:59 PM