0

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."}}}
  • 1
    are you getting data at site collection level or at subsite level ? – Gautam Sheth May 4 '18 at 15:29
  • 1
    For a POST you need to include the authorization. – Rothrock May 4 '18 at 15:30
  • @GautamSheth at sub site.. the managed metadata column itself is defined inside the site collection, while the list i am querying LookupList is at the subsite level. – john Gu May 4 '18 at 23:14
  • @Rothrock so can you advice how i need to specify the header? based on my knowledge to specify the header i need the username and password.. and i am not going to expose those values (mainly the password) inside the javascript... or the authorization will be the same as the current login user ? – john Gu May 4 '18 at 23:18
  • There are two ways to get the header. One is to make a POST call (this POST doesn't need a valid digest to work.) to <site>_api/contextinfo and get FormDigestValue from that. The other is to read it from the page (I use jQuery) $("#__REQUESTDIGEST").val(). Either way you then include "X-RequestDigest":whatever-the-value in your post headers. – Rothrock May 8 '18 at 14:59
1

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

  • you code worked well, but i have to add this to the ajax call contentType : "application/json;odata=verbose", – john Gu May 8 '18 at 14:36
2

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.

  • thanks for the reply, now defining the field as multi-select is not a good choice in my case.. now i try to using the POST appraoch but it did not work as shown inside my question.. – john Gu May 4 '18 at 23:21
  • so will try accessing the list TaxonomyHiddenList ,... but the managed metadata column itself is defined inside the site collection, while the list i am querying LookupList is at the subsite level. so will querying the TaxonomyHiddenList work? – john Gu May 4 '18 at 23:27
  • @Rothrok now i think in my case i need to query the TaxonomyHiddenList first and filter using a field named Path + select the GUID as follow /items?$filter=Path eq 'ABC'&$select=GUID. now i will have the GUID, after that i can filter the LookupList, using the GUID,, but when i try something as follow /_api/web/lists/getbytitle('LookupList')/items?$select=OrderName/TermGuid,Percentage&$expand=OrderName i got this error The field or property 'OrderName' does not exist. – john Gu May 5 '18 at 0:28
  • Not sure why you need to $expand. In your first example the termguid was included. However I would filter by the WssId a lot shorter and easier to work with. – Rothrock May 8 '18 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.