I am looking for SP Rest api to return all Terms and its GUID under specific termsets in my SP online Site and in my On Prem site. Is it possible?
if not Rest API, is it possible to pull them using client Context?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityYou could just use the pnp cmdlet Get-PnPTerm
to get all Terms and its GUID under specific termsets. It's very easy.
Read here for more:
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpterm?view=sharepoint-ps
The rest 2.1 API supports CRUD operations on the TermStore. There is an useful write-up of accessing the terms located here: https://www.c-sharpcorner.com/blogs/sharepoint-managed-metadata-services-rest-api
List Terms in Term Set
Endpoint : Endpoint : https://testinglala.sharepoint.com/_api/v2.1/termStore/groups/122d92ab-d7a8-479f-9888-8d4421f8218f/sets/8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f/terms
Method : GET
Headers : {"Content-Type": "application/json",
"Authorization": "Bearer <Bearer Token>"}
List Terms Nested Under Term in Term Set
Endpoint : https://testinglala.sharepoint.com/_api/v2.1/termStore/groups/122d92ab-d7a8-479f-9888-8d4421f8218f/sets/8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f/terms/12345678-1234-1234-1234-123456789012/children
Method : GET
Headers : {"Content-Type": "application/json",
"Authorization": "Bearer <Bearer Token>"}
Not sure if this can be done with REST but it's certainly possible with Client Context. The process should be the same for On Prem or Online.
Also, if you just want to get this information for reference, PowerShell is probably a better way to get it. The PowerShell cmdlets for SP Taxonomy make it easy to export all term labels and GUIDs to a CSV file.
Below is some JS that will print out the name and GUID for all terms in the given term set(s). This must be run from a page that has access to the SP object (i.e. a page in SharePoint).
var termSetArray = ["ce7f5d96-f7eb-45c3-83ef-062b6bdbf722"];
termSetArray.forEach(function(termSetId){
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
SP.SOD.registerSod("sp.taxonomy.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.taxonomy.js"));
SP.SOD.executeFunc("sp.taxonomy.js", "SP.Taxonomy.TaxonomySession",
function () {
// Get Context and load terms
var ctx = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
var termStore = taxSession.getDefaultSiteCollectionTermStore();
var termSet = termStore.getTermSet(termSetId);
var terms = termSet.getAllTerms();
ctx.load(terms);
// Iterate through Terms
ctx.executeQueryAsync(function () {
var termEnum = terms.getEnumerator()
while (termEnum.moveNext()) {
var currentTerm = termEnum.get_current();
var name = currentTerm.get_name();
var id = currentTerm.get_id().toString();
console.log('Term: ', name, id);
}
}, function () { console.log('error') });
});
});
});
A quick Google search for "sharepoint rest taxonomy" turned up the article here that gives an example of using an internal REST endpoint to get terms from a term set. To quote that article:
Below is quite a simple approach ( which Microsoft hasn’t documented to use for external applications ) to query term store using REST API.
[...]
Its for you to take a decision to use it or not.
Its a simple POST request to one of the internal service (which will probably not be taken out by MS because they happen to use it internally). Below is sample code to get all terms from a term set.
and the code example given is:
$.ajax({
url: "https://<your-tenant>.sharepoint.com/_vti_bin/TaxonomyInternalService.json/GetChildTermsInTermSetWithPaging",
type: "POST",
headers: {
"accept": "application/json;odata.metadata=minimal",
"content-type": "application/json;charset=utf-8",
"odata-version": "4.0",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
data: JSON.stringify({
lcid: 1033,
sspId: "guid-here", //Term store ID
guid: "guid-here", //Term set ID
includeDeprecated: false,
pageLimit: 1000,
pagingForward: false,
includeCurrentChild: false,
currentChildId: "00000000–0000–0000–0000–000000000000",
webId: "00000000–0000–0000–0000–000000000000",
listId: "00000000–0000–0000–0000–000000000000"
}),
success: function (data) {
console.log("Rejoice, for now you can use REST", data);
}
})
Now, I haven't tried it, so YMMV. The article also links to this MS documentation page which gives other method names / endpoints you might be able to use.
CSOM and AJAX calls are not recommended within SPFx apps - as these are older methods so will probably be depreciated.
I know the post replies are from two years ago, but perhaps the forum supervisors need to clean up out-of-date posts, so incorrect information is not still available. Like the two links provided by Dylan Cristy to no longer supported methods.
The _api/v2.1/termStore REST endpoint is the recommended method to get termstore metadata and can be used in No Framework or React JS framework apps.