0

Any ideas on trying to find/search users by id in Sharepoint by a REST query?

At the moment I am playing around with the clientside people picker to search for users https://msdn.microsoft.com/en-us/library/office/jj713593.aspx

I can also use this endpoint to search for users by their id.

    /_api/web/siteusers?$filter=substringof( '" + id + "',LoginName)

To get something like this

    "Id": 2,
    "LoginName": "i:0#.w|domain\\id",
    "Title": "Generic Name",
    "Email": "[email protected]",

But that only works for users that are in the siteusers and not everyone in AD.

I have also tried searching through this.

/_api/search/query?querytext='AccountName=domain\\id'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'

But I'm not sure exactly the data is returning since look likes it's in rows and it doesn't look like it's returning any names when looked at it in a REST console.

6 Answers 6

4

You can use following end-point. It can search users from all directories.

var userSearchSuggestionEndpoint = "/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser";

HTTP post request is needed to get data from above end-point. Request payload is given below.

var data = {
    queryParams: {
        __metadata: {
            type: 'SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters'
        },
        AllowEmailAddresses: true, 
        AllowMultipleEntities: false, 
        AllUrlZones: false, 
        MaximumEntitySuggestions: 50, 
        PrincipalSource: 15, 
        PrincipalType: 15,
        QueryString: "Your search string"
        Required: false, 
        SharePointGroupID: null, 
        UrlZone: null,
        UrlZoneSpecified: false, 
        Web: null,
        WebApplicationID: null
    }
}

See more about SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters in MSDN.

It does not provide user Id. That's why I personally like following end-point.

var userSearchSuggestionEndpoint = "/_vti_bin/ListData.svc/UserInformationList?
        $select=Id,Name&$filter=substringof('" + searchKey + "',Name)";

I have a complete documentation above client side custom people picker. You can check it out from here.

2
  • This is a pretty interesting endpoint but it still does the same as /_api/web/siteusers except that I can grab more detailed information from the user. I'm trying to find a restpoint that is searching through users like how the microsoft clientside people picker is doing. Because right now my siteusers don't have the same amount of users as the ones in AD. Maybe this is a different problem or something idk... Commented Apr 19, 2016 at 19:08
  • For updating a "person/people" field column, you will need the SPuser ID value for that target user, in that current site.
    – Alberto S.
    Commented Dec 21, 2017 at 11:29
1

A little old, but I ran into a similar question these days. This is how I solved it.

The method you are looking for is getproperties for. It's described here

https://msdn.microsoft.com/en-us/library/office/dn790354.aspx#bk_PeopleManagerGetPropertiesFor

Now I had a little difficulty getting the account name for my environment since it doesn't have to be the LoginName. So first to check out the syntax for your accountName I'd use

$.ajax({
        url: siteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" }

    });

Don't forget to escape if the output is something like "domain\username". After that the right call would look like this.

$.ajax({
        url: siteUrl + "_api/getpropertiesfor(@v)?@v='DOM\\USERNAME'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" }

    });
1

you need to ensure your user on on a site like this - then when a success callback is called, you can query site users endpoint. that way, you will get the id and everything else you need.

var ensureSPUser = function(loginName, callbackSucccess, callbackfailed) {
  var context, usr;
  context = SP.ClientContext.get_current();
  usr = context.get_web().ensureUser(loginName);
  context.load(usr);
  context.executeQueryAsync(callbackSucccess, callbackfailed);
};
0

To get AD users in Sharepoint you have to put filter as

/_api/web/siteusers?$select=Id,Title,LoginName,Email&$filter=id eq 'Your Id'
2
  • This looks similar to what I tried at the top already and it does find people that are on the site but not all of the people from AD. Like in the clientside people picker I can search for anyone everyone and they show up, but when I use just the /_api/web/siteusers endpoint only a few people are showing up on that site. Commented Apr 19, 2016 at 19:05
  • Same thing happen here. I cant find the way to the the userID even though I have tried both methods.
    – Alberto S.
    Commented Dec 20, 2017 at 19:57
0

Try this:

var murl = _spPageContextInfo.webAbsoluteUrl.split('/sites')[0];

url: murl + /_api/web/siteusers //(filter accordingly).

Worked for me as I was not able to get the user in my Site Collection, I got the details from Web Application.

0

I would suggest you to either:

Regarding how to get the SPuser ID for certain site ( in order to update a people field column for instance ), just use the ensureUser webmethod. Something like this:

        $.ajax({
        url: "http://[website]/_api/web/ensureuser('"+user_name+"')",
        type: "POST",
        headers: {             
            'accept': 'application/json;odata=verbose;charset=utf-8',
            'Content-Type': 'application/json;odata=verbose;charset=utf-8',
            'X-RequestDigest': $("#__REQUESTDIGEST").val()                    
        },
        success: function(response_data){ [your custom success action]
        },
        error: function(response_data){[your custom fail action]}
      });

var user_name will contain target AD username.

NOTE:

  • When using the ensureUser method against an AD user, just use its username. So, user_name would be something like this: "username". No need to add "domain\" before neither any other kind of prefix/suffix. Just the username

  • ContentType & accept headers MUST be application/json;odata=verbose;charset=utf-8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.