Search code examples
restsharepoint-2013sharepoint-api

How to get Sharepoint user by Title using REST?


I'm trying to search for a given user on a Sharepoint site using "lastname, firstname". I was able to use the following url to get the total list of site users as a large XML document:

https://thissite.com/sites/thatsite/_api/web/siteusers/

However, I haven’t been able to figure out how to search by Title. When I use the following url:

https://thissite.com/sites/thatsite/_api/web/siteusers/getbytitle(“lastname,%20firstname”)

I get this error:

<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>
-1, Microsoft.SharePoint.Client.InvalidClientQueryException
</m:code>
<m:message xml:lang="en-US">
The expression "web/siteusers/getbytitle("lastname, firstname")" is not valid.
</m:message>
</m:error>

When I use the following url to get the same user's data:

https://thissite.com/sites/thatsite/_api/web/siteusers/getbyid(41)

Then I successfully get XML returned with that user's data.

I guess I could just parse the list obtained from /siteusers and load it into a searchable data object, but I was hoping for something more direct.


Solution

  • UserCollection resource does not expose getbytitle method, that's the reason why you get this exception.

    In order filter user by title you could utilize $filter query option as demonstrated below:

    https://contoso.sharepoint.com/_api/web/siteusers?$filter=Title eq '<Title>'