SharePoint Online / Office 365 - Exporting All the Terms from Particular TermSet In .CSV File Using CSOM And Console Application

There are multiple approaches to exporting the terms from given TermSet, such as - using PowerShell + CSOM or using CSOM in console application. In one of my previous articles, “SharePoint Online / Office 365 : Exporting All The Terms From Particular TermSet In .CSV File Using CSOM & PowerShell”, I explained how to export the TermSet using CSOM + PowerShell.

In this article, I’ll go through step by step procedure for exporting TermSet using CSOM in console application.

There is no big difference in both the approaches but if we want to make it more configurable (like configuration in .config file, for ex. – TermStore name or specific TermSet name etc. ), then Console Application approach is preferable.

I have my trial Office 365 account and Term Store Manager page looks like the below images, where three terms (IT, HR and Finance) are created in OOB termset “Department” under “People” group,

TermStore
Figure 1 - My TermStore – Created three different terms – IT, HR and Finance

So, in our console application, we will export the above three terms created under “Department” termset in “People” group, in default Term Store (since there is only one Term Store).

Step 1

Get the required details from config file (App.config) and connect to Office 365.

In last article “Office 365 / SharePoint Online - Connecting Office 365 / SharePoint Online Site Using CSOM (Client Object Model)” I have given detailed steps to connect Office 365 through CSOM and Console Application. Please have a look once. Following is the code snippet for this step.

  1.        #region Site Details - Read the details from config file  
  2.             string siteURL = ConfigurationManager.AppSettings["siteURL"];  
  3.             string userName = ConfigurationManager.AppSettings["userName"];  
  4.             string password = ConfigurationManager.AppSettings["password"];  
  5.             //Path where we need to create the .CSV file  
  6.             string csvFilePath = ConfigurationManager.AppSettings["csvfilepath"];  
  7.        #endregion  
  8.  
  9.     #region Connect To O365  
  10.   
  11.             //Create the client context object and set the credentials  
  12. //Create the client context object and set the credentials  
  13.     ClientContext clientContext = new ClientContext(siteURL);  
  14.        SecureString securePassword = new SecureString();  
  15.   
  16. foreach (char c in password.ToCharArray())       
  17.     securePassword.AppendChar(c);  
  18.   
  19.                      clientContext.Credentials = new     
  20.                      SharePointOnlineCredentials(userName, securePassword);  
  21.         #endregion  

Step 3

Get the Terms from respective TermSet : To read the Terms from respective TermSet, we need to get the reference for the following objects. 

  1. TaxonomySession
    This object is the initial point for all taxonomy operations. Get reference to the TaxonomySession object as -
    1. TaxonomySession taxonomysession = TaxonomySession.GetTaxonomySession(clientContext);  
  1. TermStore
    Get reference to default TermStore. Once we have TaxonomySession class, we will get the default term store as -
    1. TermStore termStore = taxonomysession.GetDefaultSiteCollectionTermStore();  
  1. TermGroupCollection
    We will fetch all the groups (TermGroupCollection) from TermStore object as -
    1. TermGroupCollection groupCollection = termStore.Groups;  
    2. clientContext.Load(groupCollection);  
    3. clientContext.ExecuteQuery();  
  1. TermGroup
    Then, get the reference to the TermGroup object in which our TermSet is created. Here, we are fetching the terms from the term set which is created under group “People” as -
    1. TermGroup termGroup = groupCollection.GetByName("People");  
    2. clientContext.Load(termGroup);  
    3. clientContext.ExecuteQuery();  
  1. TermSet
    From termGroup object we will read our “Department” term set as -
    1. TermSet termSet = termGroup.TermSets.GetByName("Department");  
    2. clientContext.Load(termSet);  
    3. clientContext.ExecuteQuery();  
  1. TermCollection
    We have our term set ready, get all terms in it using TermCollection object as -
    1. TermCollection departmentTerms = termSet.Terms;  
    2.               clientContext.Load(departmentTerms);  
    3.        clientContext.ExecuteQuery();  

Step 4

Write terms to .CSV file : As now we have all “Department” terms, we will write them to .CSV file. To write into .CSV file, we need array of string and we will use Syste.IO.File object as -

  1. ArrayList termsArrayList = new ArrayList(departmentTerms.Count);  
  2.   
  3.               foreach (Term term in departmentTerms)  
  4.               {  
  5.                  termsArrayList.Add(term.Name);  
  6.               }  
  7.   
  8.         //Writing to .CSV file as path specified in .config file.  
  9. System.IO.File.WriteAllLines(csvFilePath,           (string[])termsArrayList.ToArray(typeof(string)));  

Complete Code

  1. using Microsoft.SharePoint.Client;  
  2. using Microsoft.SharePoint.Client.Taxonomy;  
  3. using System;  
  4. using System.Collections;  
  5. using System.Configuration;  
  6. using System.Security;  
  7.   
  8. namespace CSOM_ExportTermSets  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             #region Site Details - Read the details from config file  
  15.             string siteURL = ConfigurationManager.AppSettings["siteURL"];  
  16.             string userName = ConfigurationManager.AppSettings["userName"];  
  17.             string password = ConfigurationManager.AppSettings["password"];  
  18.             //Path where we need to create the .CSV file  
  19.             string csvFilePath = ConfigurationManager.AppSettings["csvfilepath"];  
  20.             #endregion  
  21.  
  22.             #region ConnectTo O365  
  23.   
  24.             //Create the client context object and set the credentials  
  25.             ClientContext clientContext = new ClientContext(siteURL);  
  26.             SecureString securePassword = new SecureString();  
  27.   
  28.             foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);  
  29.   
  30. clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);  
  31.  
  32.             #endregion  
  33.  
  34.             #region Get the Terms  
  35.   
  36. TaxonomySession taxonomysession = TaxonomySession.GetTaxonomySession(clientContext);  
  37.        
  38.             if (taxonomysession != null)  
  39.             {  
  40.  TermStore termStore = taxonomysession.GetDefaultSiteCollectionTermStore();  
  41.                 if (termStore != null)  
  42.                 {  
  43.                     TermGroupCollection groupCollection = termStore.Groups;  
  44.                     clientContext.Load(groupCollection);  
  45.                     clientContext.ExecuteQuery();  
  46.   
  47.                     TermGroup termGroup = groupCollection.GetByName("People");  
  48.                     TermSet termSet = termGroup.TermSets.GetByName("Department");  
  49.   
  50.                     clientContext.Load(termGroup);  
  51.                     clientContext.Load(termSet);  
  52.                     clientContext.ExecuteQuery();  
  53.   
  54.                     TermCollection departmentTerms = termSet.Terms;  
  55.                     clientContext.Load(departmentTerms);  
  56.                     clientContext.ExecuteQuery();  
  57.   
  58.                     ArrayList termsArrayList = new ArrayList(departmentTerms.Count);  
  59.   
  60.                     foreach (Term term in departmentTerms)  
  61.                     {  
  62.                         termsArrayList.Add(term.Name);  
  63.                     }  
  64.   
  65.                     //Writting to .CSV file as path specified in .config file.  
  66. System.IO.File.WriteAllLines(csvFilePath, (string[])termsArrayList.ToArray(typeof(string)));  
  67.   
  68. Console.WriteLine("Terms are written in .CSV file. Please hit the any key to exit the console");  
  69.                     Console.ReadKey();  
  70.                 }//if (termStore != null)  
  71.             }//if (taxonomysession != null)  
  72.             #endregion  
  73.         }//main  
  74.     }//cs  
  75. }//ns