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,
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.
- #region Site Details - Read the details from config file
- string siteURL = ConfigurationManager.AppSettings["siteURL"];
- string userName = ConfigurationManager.AppSettings["userName"];
- string password = ConfigurationManager.AppSettings["password"];
-
- string csvFilePath = ConfigurationManager.AppSettings["csvfilepath"];
- #endregion
-
- #region Connect To O365
-
-
-
- ClientContext clientContext = new ClientContext(siteURL);
- SecureString securePassword = new SecureString();
-
- foreach (char c in password.ToCharArray())
- securePassword.AppendChar(c);
-
- clientContext.Credentials = new
- SharePointOnlineCredentials(userName, securePassword);
- #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.
- TaxonomySession
This object is the initial point for all taxonomy operations. Get reference to the TaxonomySession object as -
- TaxonomySession taxonomysession = TaxonomySession.GetTaxonomySession(clientContext);
- TermStore
Get reference to default TermStore. Once we have TaxonomySession class, we will get the default term store as -
- TermStore termStore = taxonomysession.GetDefaultSiteCollectionTermStore();
- TermGroupCollection
We will fetch all the groups (TermGroupCollection) from TermStore object as -
- TermGroupCollection groupCollection = termStore.Groups;
- clientContext.Load(groupCollection);
- clientContext.ExecuteQuery();
- 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 -
- TermGroup termGroup = groupCollection.GetByName("People");
- clientContext.Load(termGroup);
- clientContext.ExecuteQuery();
- TermSet
From termGroup object we will read our “Department” term set as -
- TermSet termSet = termGroup.TermSets.GetByName("Department");
- clientContext.Load(termSet);
- clientContext.ExecuteQuery();
- TermCollection
We have our term set ready, get all terms in it using TermCollection object as -
- TermCollection departmentTerms = termSet.Terms;
- clientContext.Load(departmentTerms);
- 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 -
- ArrayList termsArrayList = new ArrayList(departmentTerms.Count);
-
- foreach (Term term in departmentTerms)
- {
- termsArrayList.Add(term.Name);
- }
-
-
- System.IO.File.WriteAllLines(csvFilePath, (string[])termsArrayList.ToArray(typeof(string)));
Complete Code
- using Microsoft.SharePoint.Client;
- using Microsoft.SharePoint.Client.Taxonomy;
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Security;
-
- namespace CSOM_ExportTermSets
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region Site Details - Read the details from config file
- string siteURL = ConfigurationManager.AppSettings["siteURL"];
- string userName = ConfigurationManager.AppSettings["userName"];
- string password = ConfigurationManager.AppSettings["password"];
-
- string csvFilePath = ConfigurationManager.AppSettings["csvfilepath"];
- #endregion
-
- #region ConnectTo O365
-
-
- ClientContext clientContext = new ClientContext(siteURL);
- SecureString securePassword = new SecureString();
-
- foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
-
- clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
-
- #endregion
-
- #region Get the Terms
-
- TaxonomySession taxonomysession = TaxonomySession.GetTaxonomySession(clientContext);
-
- if (taxonomysession != null)
- {
- TermStore termStore = taxonomysession.GetDefaultSiteCollectionTermStore();
- if (termStore != null)
- {
- TermGroupCollection groupCollection = termStore.Groups;
- clientContext.Load(groupCollection);
- clientContext.ExecuteQuery();
-
- TermGroup termGroup = groupCollection.GetByName("People");
- TermSet termSet = termGroup.TermSets.GetByName("Department");
-
- clientContext.Load(termGroup);
- clientContext.Load(termSet);
- clientContext.ExecuteQuery();
-
- TermCollection departmentTerms = termSet.Terms;
- clientContext.Load(departmentTerms);
- clientContext.ExecuteQuery();
-
- ArrayList termsArrayList = new ArrayList(departmentTerms.Count);
-
- foreach (Term term in departmentTerms)
- {
- termsArrayList.Add(term.Name);
- }
-
-
- System.IO.File.WriteAllLines(csvFilePath, (string[])termsArrayList.ToArray(typeof(string)));
-
- Console.WriteLine("Terms are written in .CSV file. Please hit the any key to exit the console");
- Console.ReadKey();
- }
- }
- #endregion
- }
- }
- }