How to Get All the Terms under a TermSet Programmatically using CSOM C# in SharePoint Office 365

Sathish Nadarajan
 
Solution Architect
January 23, 2017
 
Rate this article
 
Views
9253

 

As part of Auditing for one of our customer, we needed a piece of code which should get all the terms under a termset. It is a straight forward and thought of sharing with the community.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Microsoft.SharePoint.Client.Taxonomy;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
     using System.Threading.Tasks;
 
     class Program
     {
         static void Main(string[] args)
         {
             GetUniqueTerms();
         }
 
 
         public static void GetUniqueTerms()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://******.sharepoint.com/sites/CommunitySite/";
 
             string userName = "Sathish@*******.onmicrosoft.com";
             string password = "**********";
 
 
 
 
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 try
                 {
                     Web web = clientContext.Web;
 
 
                     clientContext.Load(web);
                     clientContext.Load(web, w => w.Title);
                     clientContext.ExecuteQuery();
 
                     var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
                     var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
                     var termGroup = termStore.GetSiteCollectionGroup(clientContext.Site, true);
                     termStore.CommitAll();
                     clientContext.Load(termGroup);
 
                     clientContext.ExecuteQueryRetry();
 
                     string outputFile = "C:\temp\terms" + ".txt";
 
                     var termSetCollection = termGroup.TermSets;
                     var results = clientContext.LoadQuery(termSetCollection.Where(t => t.Name == "<<TermSetName>>"));
                     clientContext.ExecuteQueryRetry();
 
                     var termSet = results.FirstOrDefault();
                     
                     if (termSet != null)
                     {
                         var terms = termSet.Terms;
                         clientContext.Load(terms);
                         clientContext.ExecuteQueryRetry();
 
                         foreach (var term in terms)
                         {
                             System.Console.WriteLine(term.Name);
 
                             System.IO.File.AppendAllText(outputFile, term.Name + "t" + term.Id + Environment.NewLine);
                         }
                     }
 
                 }
                 catch (Exception ex)
                 {
                     System.Console.ForegroundColor = ConsoleColor.Red;
                     System.Console.WriteLine("Exception Occured : " + ex.Message);
                     System.IO.File.AppendAllText("C:\Temp\UniqueTerms\Exception.txt", ex.Message + " - " + siteUrl + Environment.NewLine);
                 }
 
 
 
             }
 
             System.Console.WriteLine("Completed....");
             System.Console.WriteLine("Press Any Key to Exit ....");
             System.Console.ReadLine();
         }
 
 
     }
 
 }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment