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:
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.
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;
}
}
}
}
}
}
}
}
Hope this will be helpful.