Add Or Update SharePoint TermStore (Managed Metadata) In SharePoint Framework (SPFx)

Accessing SharePoint TermStore and playing with that is not a big deal in SPFx, we can do that with some tweaks. Since we don’t have direct REST API support for this service, we will be using the old JSOM method to add/edit TermStore in SharePoint Framework webpart.

Environment setup

We need to load some reference scripts to access SP.PageContext info. Please refer my article about reading SharePoint TermStore Managed Metadata In SharePoint Framework (SPFx) and follow the step by step guidelines in that to set up the environment to add/edit the TermSet.

What is Submission Policy in TermStore?

As we know, SPFx is functioning based on user context so the user can’t access anything out of his/her permission scope through SPFx app. TermStore is something related to SharePoint Admin Center, so then how do we add/edit TermStore through SPFx app?

We do have an option for that in TermStore settings as described below,

  1. Go to below URL to access TermStore settings in your tenant

  2. Expand your TermGroup and select the TermSet, in my case I have selected People/Department
The Submission Policy, which is highlighted in green  in the above picture, is the one which will allow users with fewer permissions to update the TermStore. If it is Open anyone can update the TermStore values through SPFx app. If it is Closed only the TermStore Managers can update the TermStore values.

Create a Term in SPFx

Once you are finished with the environmental setup, the next step is to read the specific TermSets like department and location, so we can add the new term value in that.

Please follow the below code snippet to read the TermSet using spPageContext load and execute methods,

  1. let spContext: SP.ClientContext = new SP.ClientContext(siteColUrl);  
  2. let taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(spContext);  
  3. let termStore = taxSession.getDefaultSiteCollectionTermStore();  
  4. let termGroups = termStore.get_groups();  
  5. let termGroup = termGroups.getByName("People");  
  6. let termSets = termGroup.get_termSets();  
  7. let termSet = termSets.getByName("Location");  
  8. let lcID= 1033; // Mention the lcid as per your requirement  

We got the TermSet in our SPFx instance now, the next step is to add a new Term value which is done by using the createTerm method in TermSet like below,

  1. let newTerm = termSet.createTerm(“New Term Value”, lcID, SP.Guid.newGuid());  
  2. spContext.load(newTerm);  
  3. spContext.executeQueryAsync(():void =>//Function to handle execution result);  

Please refer to this official documentation about TermSet class members to know the other options available to manipulate TermSet.

Edit/Update a Term value in SPFx

First, we need to load the all Terms from the TermSet to edit any Term, again we are going to use spPageContext load and execute methods.

  1. let termSet = termSets.getByName("Location");  
  2. let terms = termSet.getAllTerms();  
  3. spContext.load(terms);  
  4. spContext.executeQueryAsync(function() {  
  5.             var termsEnum = terms.getEnumerator();  
  6.             let termLocation: any[] = [];  
  7.             while (termsEnum.moveNext()) {  
  8.                 var spTerm = termsEnum.get_current();  
  9.                 termLocation.push({  
  10.                     label: spTerm.get_name() ID: spTerm.get_id(),  
  11.                     Term: spTerm  
  12.                 });  
  13.             }  
  14.             let termToUpdate: termLocation.find(o => o.label == ’New Term Value’);  
  15.             if (termToUpdate != null) {  
  16.                 termToUpdate.set_name(‘Edited Term Value’);  
  17.                 spContext.load(termToUpdate);  
  18.                 spContext.executeQueryAsync((): void => //Function to handle execution result);  
  19.                 }  
  20.             });  

In the above code snippet, we have loaded all the terms in an array called termLocation. I found some specific terms to update and updated the value by using set_name method of the term.

If you have any questions/issues about this article, please let me know in the comments.