How to Get Site Collection Taxonomy Group using CSOM in SharePoint 2013

I have a site collection http://c4968397007/ in which I have created a taxonomy group. Navigate to the site collection http://c4968397007/. Click on Settings, and then click on Site Settings.

Click on Term store management which is available under Site Administration section. You could be able to see the site collection taxonomy group in the Taxonomy Term Store. In this blog you will see how to get that site collection taxonomy group using client side object model.

Steps Involved:

  1. Open Visual Studio 2012 (Run as administrator).
  2. Go to File=> New => Project.
  3. Select Console Application in the Visual C# node from the installed templates.
  4. Enter the Name and click on Ok.
  5. In the solution explorer, right click on References folder and then click on Add Reference.
  6. Add the following assemblies from 15 hive (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI).
    • Microsoft.SharePoint.Client.dll
    • Microsoft.SharePoint.Client.Runtime.dll
    • Microsoft.SharePoint.Client.Taxonomy.dll
       
  7. Open Program.cs file and replace the code with the following
     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Collections.ObjectModel;

    using Microsoft.SharePoint.Client;

    using Microsoft.SharePoint.Client.Taxonomy;

     

    namespace TaxonomyCodeSamples

    {

        class Program

        {

            static void Main(string[] args)

            {

                // ClienContext - Get the context for the SharePoint Site

     

                // SharePoint site URL - http://c4968397007/

     

                ClientContext clientContext = new ClientContext("http://c4968397007/");

     

                // Get the TaxonomySession

     

                TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);

     

                // Get the term store by name

     

                TermStore termStore = taxonomySession.TermStores.GetByName("MMS");

     

                // Returns the associated site collection group for the SPSite.

     

                TermGroup termGroup = termStore.GetSiteCollectionGroup(clientContext.Site, false);

     

                clientContext.Load(termGroup);

     

                clientContext.ExecuteQuery();

     

                // Display the group name

     

                Console.WriteLine(termGroup.Name);

     

                Console.ReadLine();

            }

        }

    }