Retrieve term store data programmatically using csom in SharePoint Online

This SharePoint csom tutorial, we will discuss how to retrieve term store data programmatically using csom in SharePoint Online. We will retrieve term store group, term set name and terms from term store using .Net managed object model in SharePoint Online Office 365.

Also, we will see, how to retrieve term store data including Labels using csom (.Net managed object model) in SharePoint Online.

Retrieve term store data programmatically using csom in SharePoint

Here we have a term store in Office 365 and it has a group, term set and few term sets in SharePoint. The structure looks like below:

programmatically retrieve terms label from term store

Now we will see how we can retrieve groups, term sets and terms using csom (.Net managed client object model). Here we have taken a windows application. To work with SharePoint objects we need to add the below details:

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime
  • Microsoft.SharePoint.Client.Taxonomy

Below is the full code to retrieve the term store group, term set name, and terms from the SharePoint term store.

private void button1_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
// Use default authentication mode.
context.AuthenticationMode = ClientAuthenticationMode.Default;
var secureString = new SecureString();
foreach (char c in "********")
{
secureString.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials("*******@onlysharepoint2013.onmicrosoft.com", secureString);
try
{
getTerms(context);
}
catch (Exception ex)
{
throw;
}
}
}

public void getTerms(ClientContext cc)
{
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc);
TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
cc.Load(termStore,
store => store.Name,
store => store.Groups.Include(
group => group.Name,
group => group.TermSets.Include(
termSet => termSet.Name,
termSet => termSet.Terms.Include(
term => term.Name
)
)
)
);

cc.ExecuteQuery();
string s = string.Empty;
if (taxonomySession != null)
{
if (termStore != null)
{
foreach (TermGroup group in termStore.Groups)
{
if (group.Name == "Company Term Store")
{
lblTermGroupName.Text ="Term Store Group Name: "+ group.Name;
foreach (TermSet termSet in group.TermSets)
{
lblTermSets.Text = "Term Sets Name: " + termSet.Name;
foreach (Term term in termSet.Terms)
{
s += term.Name +"\n";
}
lblTerms.Text = s;
}
}
}
}
}
}

Once we run the code and click on the button click, we can see the details like below:

sharepoint get term store programmatically using csom

Retrieve term store data including Labels using csom in SharePoint

Now, we will see how to retrieve term store data including labels using .Net managed object model in SharePoint Online.

In SharePoint online term store I have made the structure like below. Here also I have added the data in Other Labels. Here we will see how to retrieve groups, term sets, terms and labels using csom (.Net managed client object model). The term store looks like below:

programmatically retrieve terms from term store

Here we are going to write our code in a windows application. So first create a windows application and then add the below ddls in the reference.

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime
  • Microsoft.SharePoint.Client.Taxonomy

Below is the full code to retrieve term store data including Labels using csom in SharePoint Online programmatically.

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientOMDemo
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
// Use default authentication mode.
context.AuthenticationMode = ClientAuthenticationMode.Default;
var secureString = new SecureString();
foreach (char c in "*******")
{
secureString.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials("*****@onlysharepoint2013.onmicrosoft.com", secureString);
try
{
getTerms(context);
}
catch (Exception ex)
{
throw;
}
}
}

public void getTerms(ClientContext clientContext)
{
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
clientContext.Load(termStore,
store => store.Name,
store => store.Groups.Include(
group => group.Name,
group => group.TermSets.Include(
termSet => termSet.Name,
termSet => termSet.Terms.Include(
term => term.Name
)
)
)
);
clientContext.ExecuteQuery();
string s = string.Empty;
string lblCol= string.Empty;
if (taxonomySession != null)
{
if (termStore != null)
{
foreach (TermGroup group in termStore.Groups)
{
if (group.Name == "Company Term Store")
{
lblTermGroupName.Text ="Term Store Group Name: "+ group.Name;
foreach (TermSet termSet in group.TermSets)
{
lblTermSets.Text = "Term Sets Name: " + termSet.Name;
foreach (Term term in termSet.Terms)
{
s += term.Name + "\n";
clientContext.Load(term.Labels);
clientContext.ExecuteQuery();
LabelCollection lblCollection= term.Labels;
lblCol +="Default Label:: "+ lblCollection[0].Value.ToString() + " Other Label:: " + lblCollection[1].Value.ToString() + "\n";
}
lblTerms.Text = s;
lblLabel.Text = lblCol;
}
}
}
}
}
}
}
}

Once you run the code, you can see the term store details like below:

sharepoint get term store programmatically

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to retrieve term store data including Labels programmatically using csom (.Net managed object model) in SharePoint Online Office 365.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

>