Retrieve term store data including Labels using .Net managed object model in SharePoint Online

Retrieve term store data including Labels using .Net managed object model in SharePoint Online

In this post we will discuss how we can 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 we can retrieve groups, term sets, terms and labels using .Net managed client object model. The term store looks like below:

Retrieve term store data including Labels using .Net managed object model in SharePoint Online
Retrieve term store data including Labels using .Net managed object model in SharePoint Online

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

Also you can check few useful posts:
How to remove or hide Replace It button from file upload dialog box in SharePoint online?

How to deploy WebJob to Microsoft Azure using Upload zip file method in SharePoint Online?

Steps to create a provider hosted add-in in SharePoint Online and host in Microsoft Azure using Import method

Below is the full code.

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:

Retrieve term store data including Labels using .Net managed object model in SharePoint 2013
Retrieve term store data including Labels using .Net managed object model in SharePoint 2013

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →