Programmatically get the site collection taxonomy group 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, 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 groupusing server object model.

 

TermStore.png


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).
    1. Microsoft.SharePoint.dll
    2. Microsoft.SharePoint.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 Microsoft.SharePoint;

using Microsoft.SharePoint.Taxonomy;

 

namespace TaxonomyCodeSamples

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite site = new SPSite(http://c4968397007/))

            {

                TaxonomySession taxSession = new TaxonomySession(site);

                TermStore termStore = taxSession.TermStores["MMS"];

                Group group = termStore.GetSiteCollectionGroup(site);

                Console.WriteLine(group.Name);

                Console.ReadLine();

            }

        }

    }

} 

 

Site Collection - c4968397007 will be the output.

[Note: Site Collection http://c4968397007/ is associated with MMS service application].