The SharePoint item REST API has its limits. One (or two in this case) of these limits is about returning values for fields you specified, more in particular when using lookup- or managed metadata fields.
The problem with the lookup field occurs when it references a list from another web. In that case, the REST API will return an error message.
Info: This issue has already been reported and a UserVoice request has been created a while ago: Enable support for lookup columns in other webs in the REST API.
Besides this lookup field problem, there is also an issue when using a managed metadata field which is configured to single term selection. Only when the field is configured to allow single term selection, the REST API returns the index of the term instead of the actual term label value.
Info: also for this there is a UserVoice request: Fix the REST API to return the value and not the Id when getting single value Managed Metadata field.
When you configure the field to be a multi-select term field, it returns the right labels.
Using the RenderListDataAsStream API
Update: The documentation of this endpoint has been added to the SharePoint development documentation: Retrieve items as stream.
Normally you would use the following API endpoint to retrieve item information:
|
|
As mentioned before, when using a lookup field, the endpoint returns an error message.
When using a single selection managed metadata term, you get the index returned instead of the label of the term. You can also see this in the above screenshot. The label I retrieved is 5 and I expected to get “Home”.
In order to solve both issues, you can make use of the RenderListDataAsStream endpoint. This endpoint allows you to specify CAML queries (back to the “good” old days). The API endpoint only allows you to call it via POST requests.
Info: More information about this endpoint can be found here: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.renderlistdataasstream.aspx
Here is an example of how you could use the API:
In case of a lookup field, you get the following response back:
No more error message, you can see the lookup field value highlighted in the above screenshot.
For the managed metadata term field value, you get this in return:
The label now contains the label of the term.
A sample of using the API in SPFx
Here is the sample using the API in an SPFx solution: