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


Sathish Nadarajan
SharePoint MVP
Published On :   23 Jan 2017
Visit Count
Today :  6    Total :   1780
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


 

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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan