Question

I am working on SharePoint on-premises farm 2013 and I have a list named "LookupList", which contain a managed metadata column named "OrderName". Now I want to get the term label/name which is stored inside the managed metadata column using REST API. So I tried the following api call:-

/_api/web/lists/getbytitle('LookupList')/items?$select=OrderName,Percentage

But what I got is the term GUID & term ID, but I did not get the term label/name. Here is a sample of the result I got from the above call:-

<m:properties>
<d:OrderName m:type="SP.Taxonomy.TaxonomyFieldValue">
<d:Label>38</d:Label>
<d:TermGuid>7*****9-a3*******d-b***7-c9********c</d:TermGuid>
<d:WssId m:type="Edm.Int32">38</d:WssId>
</d:OrderName>

EDIT.

Now I read that I can pass CAML query inside my rest api, so inside my JavaScript code I wrote the following Ajax:-

 $.ajax({
     url: "/order/_api/web/lists/getbytitle('LookupList')/GetItems(query=@v1)?@v1={\"ViewXml\":\"<View><Query><Where><Eq><FieldRef%20Name='OrderName'/><Value Type='TaxonomyFieldType'>123</Value></Eq></Where></Query></View>\"}",
     method: "POST",      
     async: false,
     headers: { "Accept": "application/json; odata=verbose" },
     success: function (data) {
        if(data.d.results.length>0){

            var items=data.d.results;

            for(var i=0;i<items.length;i++){                
                    alert(items[i]);
}
        }
     }  
    });

but I got this error:-

{"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}}
Was it helpful?

Solution

You are missing the Request digest. It is necessary for POST requests.

Modify the CAML query as per your requirement.

Modify code as below:

var body = { 
       'query':{ 
           '__metadata': { 'type': 'SP.CamlQuery' },
           'ViewXml': "<View><Query></Query></View>"
        } 
   };   

$.ajax({
     url: "/order/_api/web/lists/getbytitle('LookupList')/GetItems",
     method: "POST",      
     async: false,
     data: JSON.stringify(body),
     headers: { 
        "Accept": "application/json; odata=verbose",
        "contentType" : "application/json;odata=verbose",
        "X-RequestDigest": $('#__REQUESTDIGEST').val()       
     },
     success: function (data) {
        console.log(data);
        //do your stuff here
     }  
    });

Reference - Using CAML query with REST API

OTHER TIPS

There is a known bug where single select taxonomy terms will not return the expanded/human readable label.

If you can change your field to multi-select they will expand correctly. Of course that might not be a good choice for other reasons.

Otherwise you can do a couple of things.

Instead of using a GET call to the /items you can use a POST to the following

_api/web/Lists/GetByTitle('LookupList')/GetItems(query=@v1)?@v1={'ViewXml':'<View><Query></Query></View>'}"

This comes from this post. I have found others about this issue in the past but that was the first I saw today.

The other thing you could do is make two rest calls. One like you have now and then one to the HiddenTaxonomyList. The list is at the root level of your site collection so the call would be to there. So if you are on a subsite you'll need to change your scope.

<site collection/_api/web/lists/getbytitle('TaxonomyHiddenList')/items?$filter=ID eq 38&$select=ID,Title

Of course you would need to build the $filter part of the query to get the ones you were interested in.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top