10

I want to get a list, using REST, which include Managed Metadata columns. Currently I get a TermGuid for these columns, which I then have to do more work on to get their actual default values.

Is there a way to do this all in one call? Something like:

/sites/mysite/_api/web/lists/getbytitle('My List')/items?$expand=managed_metadata

2 Answers 2

8

SP.Taxonomy.TaxonomyFieldValue type exposes the following properties via SharePoint REST API:

  • Label
  • TermGuid
  • WssId

How to retrieve Taxonomy field values using SharePoint REST in JavaScript

Assume Pages library contains:

  • Category single valued taxonomy field
  • Categories mutli-valued taxonomy field

Then the following example demonstrates how to perform GET request to retrieve taxonomy field values:

function getJson(url,success,failure)
{
    $.ajax({
      type: "GET", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
      }, 
      url: url, 
      success: success,
      failure: failure 
   });
}

function getListItems(listTitle,success,failure)
{
    var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listTitle + "')/items";
    getJson(endpointUrl,
      function(data){
        success(data.d.results);    
      },
      failure);
}


//Usage
getListItems('Pages',
  function(items){
      //print info
      for(var i = 0; i < items.length; i++) {
          console.log(items[i].Category.Label);  //single-valued taxonomy field  
          if(items[i].Categories.results.length > 0) 
             console.log(items[i].Categories.results[0].Label);  //print first value of multiple taxonomy field
      }
  },
  function(error){
     console.log(JSON.stringify(error)); 
  }
);

But unfortunately there is a known bug with retrieving a Label from single-valued taxonomy field using SharePoint REST GET request.

The workaround is to perform POST request to retrieve taxonomy field values as demonstrated below:

function postJson(endpointUrl,payload,success,failure)
{
    $.ajax({
      type: "POST", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
      }, 
      data: JSON.stringify(payload),
      url: endpointUrl, 
      success: success,
      failure: failure 
   });
}


function getListItems(listTitle, queryViewXml,success,failure)
{
   var queryPayload = { 
       'query':{ 
           '__metadata': { 'type': 'SP.CamlQuery' },
           'ViewXml': queryViewXml
        } 
   }; 
   var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
   postJson(endpointUrl,queryPayload,
     function(data){
        success(data.d.results);    
     },failure);
}

//Usage
getListItems('Pages','<View><Query/></View>',
  function(items){
      //print info  
      for(var i = 0; i < items.length; i++) {
          console.log(items[i].Category.Label);  //single-valued taxonomy field  
          if(items[i].Categories.results.length > 0)  
             console.log(items[i].Categories.results[0].Label);  //print first value of multiple taxonomy field
      }
  },
  function(error){
     console.log(JSON.stringify(error)); 
  });  
4
  • What's the known bug?
    – matt
    Oct 24, 2014 at 22:01
  • 2
    You could some details, for example here:sharepointificate.blogspot.co.uk/2014/05/… Oct 24, 2014 at 22:07
  • 2
    Ah nice, I'm seeing just the number 1 for my labels as well.
    – matt
    Oct 24, 2014 at 22:26
  • 3
    MS still hasn't fixed this! So I'm guessing that I would need to translate all my select, filter, orderby, etc into CAML syntax? Or is there a way to combine the two?
    – Rothrock
    Feb 10, 2016 at 17:40
7

You can do a workaround using the TaxCatchAll field as long as you know which terms belong to which taxonomy field - if you have multiple.

/_api/web/Lists/GetByTitle('<list>')/Items/?$select=*,TaxCatchAll/Term&$filter=TaxCatchAll/Term eq 'Puzzlepart'&$expand=TaxCatchAll
7
  • 1
    I got this error The expression "*,TaxCatchAll/Term$filter=TaxCatchAll/Term eq 'termname'" is not valid. Nov 10, 2016 at 12:40
  • 1
    I missed a &. Edited my answer now to form a correct url. Dec 5, 2016 at 8:37
  • 1
    That might work only for lists at the site collection level? I tried it on a subsite and I got the following error "The field 'TaxCatchAll' is not supported in query. The lookup list is in another web." Is there a way to do that on subsites?
    – Rothrock
    Apr 26, 2017 at 19:19
  • I have no idea and never tried. We tend to use single site collections and a minimal of subsites these days. But might be that you have hit a boundary for this workaround. Apr 27, 2017 at 6:31
  • 3
    I just added TaxCatchAll/ID, which allows me to map the values in TaxCatchAll to their corresponding columns. For example: <d:MyLocation m:type="SP.Taxonomy.TaxonomyFieldValue"> <d:Label>5</d:Label> <d:TermGuid>c8cb412b-ed99-4034-8ca7-5019940ec354</d:TermGuid> <d:WssId m:type="Edm.Int32">5</d:WssId> </d:MyLocation> and <m:properties> <d:ID m:type="Edm.Int32">5</d:ID> <d:Term>Lab 2B</d:Term> </m:properties> The 5 values connects the two. I'm working with multiple MMS columns. May 9, 2017 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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