How to get username through REST
-
10-12-2019 - |
Question
I am currently working with a task where you have a SPList in one farm that holds some data that we need to aggregate into a view on another farm. Therefore I am working on a Visual Web Part that through the SharePoint 2013 REST API gets the items in the list and aggregates the information into a DataTable and then displays the information.
// Construct URI
string restUrl = string.Format(SiteCollectionUrl +
"/_api/web/lists/getByTitle('projectregister')/items" +
"?$select=LeadId");
Uri uri = new Uri(restUrl);
The list however implements a SPFieldType.User where you can pick a user, and when looking at the XML the call returns, you only get an integer. I would have just implemented a method that looks up the user id and get the username, but that will not work because we are talking about aggregating data across two different SharePoint farms.
<d:LeadId m:type="Edm.Int32">1</d:LeadId>
The internal name of the SPField is called "Lead", but somehow that does not exist when selecting it in the REST call and selecting all only returns LeadId and others not relevant to this question.
Does anyone know how to return the guid og the users name instead of that integer?
Thanks!
Solution
If you need to retrieve an additional properties of the User
field in a REST call, you can make it work by adding the projection for the User
field column. For more details please follow an article Getting User Information with the SharePoint 2013 REST API
Example:
In your case the following REST endpoint returns user ID
, Title
and Guid
for Lead
User column:
/_api/web/lists/getByTitle('projectregister')/items?$select=Lead/Id,Lead/GUID,Lead/Title&$expand=Lead/Id