How to retrieve term store data using .Net managed object model in SharePoint Online?

retrieve term store data using .Net managed object model

Here we will discuss how we can retrieve term store group, term set name and terms from term store using .Net managed object model in SharePoint online (Download PDF for FREE) Office 365. Here we have a term store in Office 365 and it has a group, term set and few term sets. The structure looks like below:

retrieve term store data using .Net managed object model
retrieve term store data using .Net managed object model

Read some sharepoint online tutorials:

SharePoint deveopment training course

Now we will see how we can retrieve groups, term sets and terms using .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:

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:

retrieve term store data using .Net managed object model sharepoint online
retrieve term store data using .Net managed object model sharepoint online

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

About 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"

View all posts by Bhawana Rathore →