Programmatically Add the Site Collections to Have Read Only Access to the Local Site Collection Taxonomy Group in SharePoint 2013.

In this article you will see how to add the site collections to have read only access to the local site collection taxonomy group in SharePoint 2013 using the server object model.

Introduction

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".
Share1.jpg
Click on "Term store management" available under the Site Administration section. You will see the local site collection taxonomy group in the Taxonomy Term Store.

Share1.jpg

In SharePoint 2013, local term sets can be exposed to other site collections. This is one of the improvements in SharePoint 2013. Please refer article to learn more about cross site collection term set access in SharePoint 2013. In this article you will see how to add the site collection URLs to have read only access to the local site collection taxonomy group using the server side object model.

Procedure

  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 the References folder and then click on "Add Reference".
  6. Add the following assemblies from hive 15 (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI).
    a. Microsoft.SharePoint.dll
    b. Microsoft.SharePoint.Taxonomy.dll
  7. Open the "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
        {
           
    staticvoid Main(string[] args)
            {
               
    //// siteURLs is List<String> variable to store the root URLs of site collection
                List<string> siteURLs = new List<string>();
                siteURLs.Add(
    "http://c4968397007/sites/Vijai");
                siteURLs.Add(
    "http://c4968397007:8080/sites/AppCatalog");
              
               
    using (SPSite site = new SPSite("http://c4968397007/"))
                {
                    TaxonomySession taxSession =
    new TaxonomySession(site);
                   
    //// Get the term store
                    TermStore termStore = taxSession.TermStores["MMS"];
                   
    //// Get the local site collection taxonomy group
                    Group group = termStore.GetSiteCollectionGroup(site);
                   
    foreach (string URL in siteURLs)
                    {
                       
    //// Adds a site collection to have read-only access to this local site collection group.
                        group.AddSiteCollectionReadOnlyAccess(URL);
                    }
     
                   
    //// Commit all the changes
                    termStore.CommitAll();
                }
            }
        }
    }

     

  8. Hit F5.
  9. Navigate to the site collection http://c4968397007/. Click on "Settings", then click on "Site Settings".
  10. Click on "Term store management" that is available under the Site Administration section. You will see the local site collection taxonomy group in the Taxonomy Term Store.
  11. Click on "Site Collection - c4968397007 taxonomy group". On the properties window under the Site Collection Access section you will see the URLs added that will have read only access to the taxonomy group.

    Share2.jpg

Summary

Thus in this article you have seen how to add the site collections to have read only access to the local site collection taxonomy group in SharePoint 2013 using server object model.

Reference

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.group.addsitecollectionreadonlyaccess.aspx

Next Recommended Readings