Share via


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

Introduction:

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 let us made the structure like below. Here also we have added the data in Other Labels also. 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:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/retrieve-term-store-data-net-managed-client-object-model.png

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

Full Code:

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:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/retrieve-term-store-data-net-managed-client-object-model-sharepoint-online.png

References:

Also you can check few useful posts on:

Conclusion:

We have discussed how we can retrieve groups, term sets, terms and labels using .Net managed client object model.