SharePoint 2013–retrieving the lookup value of User fields using the REST API

SharePoint 2013–retrieving the lookup value of User fields using the REST API

I’ve been stuck on an issue for a while trying to retrieve the value of a User field using the SharePoint 2013 REST API.

The MSDN documentation is less than helpful and offers this advice for retrieving the value of lookup fields using REST –

In addition, you can specify that the request returns projected fields from other lists and the values of lookups. To do this, specify the field name in both the $select and $expand query options. For example:

http://server/site/_api/web/lists(‘guid’)/Products?$select=Title,Products/Name&$expand=Products/Name

It seems less than clear to me what you need to specify in the $select and $expand parameters!

Doing a bit of googling turned up nothing, surely someone has worked with the REST API in SharePoint 2013?!

If you simply select all fields from the list in your REST query you will find that your field does come back but instead the name will have ‘Id’ appended to it e.g ‘fieldnameId’ and it’s value will be the users’ id in the User Information List.

So what’s the fix I hear you cry?? Well it turns out this is the syntax you need to use in your REST query:

http://server/site/_api/lists/getbytitle(‘My List’)/items/$?select=Title,MyUserField/Name&$expand=MyUserField/Name

  • MyUserField is the name of my User field and Name is the target field in the User Information List. This syntax basically tells the OData property to expand the results for MyUserField to include the value of the Name field.
  • The User Field will come back as a new object which you can interrogate and there you will see the Name property.

Untitled

Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">