O365 - Programmatically Create New Term Store Management In SharePoint Online - CSOM

Introduction

I have already explained how to create a new term store management in SharePoint UI OOTB method. This article helps you to create new Term Group using CSOM method.

This article helps you to find out how to create new term store management in SharePoint 2013 online. The SharePoint 2013 term store is a “farm” level repository of hierarchical term sets and terms used by SharePoint for both, metadata and navigation.

Prerequisites

Add the following assemblies from 15 hive (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI).

  1. Microsoft.SharePoint.Client.dll
  2. Microsoft.SharePoint.Client.Runtime.dll
  3. Microsoft.SharePoint.Client.Taxonomy.dll

To create and manage terms in the Term Store management tool, you must be a Contributor, a Group Manager, or a Term Store Administrator. To create a new term set group, you must be a Term Store Administrator. When you create a new group, it is a global group. 


Follow the beow steps to execute the code.

  1. Open Visual Studio 2012 
  2. Go to File=> New => Project.
  3. Select Console Application in the Visual C# node from the installed templates.
  4. Enter the Name and click on Ok.
  5. In the solution explorer, right click on References folder and then click on Add Reference.
  6. Add the following assemblies from 15 hive (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI).
  7. Microsoft.SharePoint.Client.dll
  8. Microsoft.SharePoint.Client.Runtime.dll
  9. Microsoft.SharePoint.Client.Taxonomy.dll
  10. Copy the below code and paste in your IDE.

Create Term Group 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using Microsoft.SharePoint.Client.Taxonomy;  
  8. using System.Collections.ObjectModel;  
  9.    
  10. namespace NewtermStoremanagement  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string termGroupName="NewTermGroup";  
  17.             ClientContext clientContext = new ClientContext("https://gowthamr-admin.sharepoint.com/");   
  18.             TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);  
  19.             TermStore termStore = taxonomySession.TermStores.GetByName("MMS");  
  20.             TermGroup termGroup = termStore.CreateGroup(termGroupName, Guid.NewGuid());   
  21.             clientContext.ExecuteQuery();  
  22.         }  
  23.     }  
  24. }   



Create New Term Set

A term set is a group of related terms.

  • Local term sets are created within the context of a site collection, and are available for use (and visible) only to the users of that site collection.
  • Global term sets are available for use across all sites that subscribe to a specific Managed Metadata Service application. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using Microsoft.SharePoint.Client.Taxonomy;  
  8. using System.Collections.ObjectModel;  
  9.    
  10. namespace NewtermStoremanagement  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.               
  17.             ClientContext clientContext = new ClientContext("https://gowthamr-admin.sharepoint.com/");   
  18.             TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);  
  19.             TermStore termStore = taxonomySession.TermStores.GetByName("MMS");              
  20.             Guid guid = new Guid("f05e3a3a-884f-4feb-c32a-49735f3fb7f7");              
  21.             TermGroup termGroup = termStore.GetGroup(guid);   
  22.             string termSetName="NewTermSet";              
  23.             Guid newTermSetGuid=Guid.NewGuid();              
  24.             int lcid=1033;               
  25.             TermSet termSetColl = termGroup.CreateTermSet(termSetName, newTermSetGuid, lcid);         
  26.             clientContext.ExecuteQuery();  
  27.         }  
  28.     }  
  29. }   



Create a Term


Conclusion

This article was to help you understand about Term Store Management in SharePoint Online using client object model The next article will describe how to create an MMS service using PowerShell method.

X

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

Start Learning Now