How To Read SharePoint TermStore (Managed Metadata) Using REST API

SharePoint itself provides a REST API for each site you create. It is such wonderful thing to help the developers manipulate maximum areas of SharePoint like accessing lists, settings, users, user groups, navigation, etc. through the application that you built.

Please refer to the REST API reference and samples from Microsoft Office Dev Center to get into the details of the possibilities through REST API.

Since there are some restrictions and threshold limits are always there in SharePoint to improve performance we cannot access some areas of SharePoint through Rest API. But still it is possible to access it by using SharePoint’s old web service methodology (until Microsoft deprecated it).

Let’s see the workaround to use SharePoint’s old web service methodology to get the TermStore Data with step by step instructions.

Step 1

Create a Managed Metadata field in any List and map the respective term set item with it, like below.

SharePoint

Step 2

Now, using REST API get the field information of the Managed Metadata column which you have created in the previous step so we could be able to get the SspId and TermSetId in the field XML of the list.

Rest API Url : {HostUrl}/_api/Web/Lists/getbytitle('{ListName}')/fields/getbytitle('{FieldInternalName}')?$select=SspId,TermSetId

The output will be like below: (SspId and TermSetId were highlighted)

 

  1. {  
  2.     "SspId""905b427f-399b-4815-bdb0-d9f8a6f829eb",  
  3.     "TermSetId""8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f",  
  4. }  

 

Note - Since we need only these two values, I have queried only those fields in ODATA query  $select=SspId,TermSetId in above url, if you want to read all field information, you can use ,*.

Step 3

Now, by passing these SspId and TermSetId, we need to make a SOAP call to invoke SharePoint web service to get the Term Set Data. Here, we are going to post the input data in XML format so please mention the content-type in the headers as “text/xml” along with the authorization headers.

The post body will look like below. Replace the highlighted values with respective ids.

WebService URL: {HostUrl}/_vti_bin/taxonomyclientservice.asmx?op=GetTermSets

Request Body

  1. <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns:xsd='http://www.w3.org/2001/XMLSchema'>  
  2.   <soap:Body>  
  3.     <GetTermSets xmlns='http://schemas.microsoft.com/sharepoint/taxonomy/soap/'>  
  4.       <sharedServiceIds>  
  5.         <sspIds><sspId>{SSPID}</sspId></sspIds>  
  6.       </sharedServiceIds>  
  7.       <termSetIds>  
  8.         <termSetIds><termSetId>{TERMSETID}</termSetId></termSetIds>  
  9.       </termSetIds>  
  10.       <lcid>1033</lcid>  
  11.       <clientTimeStamps><dateTimes><dateTime>1900-01-01T00:00:00</dateTime></dateTimes></clientTimeStamps>  
  12.       <clientVersions><versions><version>0</version></versions></clientVersions>  
  13.     </GetTermSets>  
  14.   </soap:Body>  
  15. </soap:Envelope>  

Finally, we will get the expected output in XML format and parse the XML to deserialize API response, sample output format is mentioned below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  3.   <soap:Body>  
  4.     <GetTermSetsResponse xmlns="http://schemas.microsoft.com/sharepoint/taxonomy/soap/">  
  5.       <GetTermSetsResult>  
  6.         <Container>  
  7.         <TermStore>  
  8.         <TS a9="8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f" a17="true" a11="" a16="true" a12="Department" a68="" />  
  9.         <T a9="7722fca2-757b-4428-b306-36138c756ca6" a21="false" a61="0">  
  10.         <LS>  
  11.         <TL a32="HR" a31="true" />  
  12.         </LS>  
  13.         <DS />  
  14.         <TMS>  
  15.         <TM a24="8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f" a12="Department" a40="" a17="true" a67="" a45="7722fca2-757b-4428-b306-36138c756ca6" />  
  16.         </TMS>  
  17.         </T>  
  18.         <T a9="27ca61aa-6215-493f-a8a6-3a35ed19ed51" a21="false" a61="0">  
  19.         <LS>  
  20.         <TL a32="Product Engineering" a31="true" />  
  21.         </LS>  
  22.         <DS />  
  23.         <TMS>  
  24.         <TM a24="8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f" a12="Department" a40="" a17="true" a67="" a45="27ca61aa-6215-493f-a8a6-3a35ed19ed51" />  
  25.         </TMS>  
  26.         </T>  
  27.         <T a9="e0ab7f9e-3f1f-484e-a5aa-165b7eacd6f2" a21="false" a61="0">  
  28.         <LS>  
  29.         <TL a32="Creative Head" a31="true" />  
  30.         </LS>  
  31.         <DS />  
  32.         <TMS>  
  33.         <TM a24="8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f" a12="Department" a40="" a17="true" a67="" a45="e0ab7f9e-3f1f-484e-a5aa-165b7eacd6f2" />  
  34.         </TMS>  
  35.         </T>  
  36.         </TermStore>  
  37.         </Container>  
  38.       </GetTermSetsResult>  
  39.       <serverTermSetTimeStampXml><Container><Node Time="636627502984900000" Version="1" TermId="8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f" /></Container></serverTermSetTimeStampXml>  
  40.     </GetTermSetsResponse>  
  41.   </soap:Body>  
  42. </soap:Envelope>  

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now