none
Programmatically attach a term set to site collection RRS feed

  • Question

  • Would anyone happen to know any good resources to programmatically attach a term set to a site collection? The "Navigation" page under Look & Feel will not load for me, so my last resort is to tell the site collection which term set it should use for its global navigation programmatically.

    Please, and thank you!

    Wednesday, August 7, 2013 12:14 PM

Answers

  • Hi Catherine,

    Since you are already seeing the Navigation link, I assume that the publishing features are activated since they are reqd for Managed Navigation. Now, using the following powershell code, you can easily attach an existing termset/create a new termset and attach it with your site navigation.

    $navigationSet = $group.CreateTermSet($termsetName)
     $navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
     $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_SiteId", $site.ID.ToString())
     $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_WebId", $site.RootWeb.ID.ToString())    
     $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_OriginalUrl", $site.RootWeb.Url )

    In the above code $group refers to the TermSet Group and $TermSet refers to the termset name. You can easily fetch $group using the code below -

    #Get Taxonomy Session
    $session =  new-object Microsoft.SharePoint.Taxonomy.TaxonomySession("Site URL");
    #Get Managed Metadata Service
    $Store = $Session.TermStores["Managed Metadata Service"];
    #Get Term Group
    $group = $Store.Groups["TermSet Group Name"];
    

    Narahari

    ******If a post answers/helps your question, please click "Mark As Answer" on that post and/or "Vote as Helpful*******

    Thursday, August 8, 2013 7:40 AM
  • Hi,

    Here is a .NET server managed code shows how to assign navigation settings to the NavigationTermSet object and render them on your publishing site:

    http://code.msdn.microsoft.com/office/SharePoint-2013-Assign-new-c79ce037

    update: sorry for the misunderstanding before.

    best regards



    Thursday, August 8, 2013 2:37 AM
    Moderator
  • You can programmatically attach a termset to a site-collection using SP 2013 Client Side Object Model.

    NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(context,
                    termSet2, context.Web, "GlobalNavigationTaxonomyProvider");

                navTermSet.IsNavigationTermSet = true;
                navTermSet.TargetUrlForChildTerms.Value = "~site/Pages/Topics.aspx";

                NavigationTerm navterm = navTermSet.CreateTerm(termname, NavigationLinkType.SimpleLink, Guid.NewGuid());
                navterm.SimpleLinkUrl = navurl;

    Refer my blog post for more details

    http://msmvps.com/blogs/sundar_narasiman/archive/2012/11/18/manipulate-taxonomy-objects-using-sharepoint-2013-client-object-model.aspx


    Please mark the replies as answers if they help or unmark if not.

    Thursday, August 8, 2013 10:39 AM

All replies

  • Hi,

    Here is a .NET server managed code shows how to assign navigation settings to the NavigationTermSet object and render them on your publishing site:

    http://code.msdn.microsoft.com/office/SharePoint-2013-Assign-new-c79ce037

    update: sorry for the misunderstanding before.

    best regards



    Thursday, August 8, 2013 2:37 AM
    Moderator
  • Hi Catherine,

    Since you are already seeing the Navigation link, I assume that the publishing features are activated since they are reqd for Managed Navigation. Now, using the following powershell code, you can easily attach an existing termset/create a new termset and attach it with your site navigation.

    $navigationSet = $group.CreateTermSet($termsetName)
     $navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
     $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_SiteId", $site.ID.ToString())
     $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_WebId", $site.RootWeb.ID.ToString())    
     $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_OriginalUrl", $site.RootWeb.Url )

    In the above code $group refers to the TermSet Group and $TermSet refers to the termset name. You can easily fetch $group using the code below -

    #Get Taxonomy Session
    $session =  new-object Microsoft.SharePoint.Taxonomy.TaxonomySession("Site URL");
    #Get Managed Metadata Service
    $Store = $Session.TermStores["Managed Metadata Service"];
    #Get Term Group
    $group = $Store.Groups["TermSet Group Name"];
    

    Narahari

    ******If a post answers/helps your question, please click "Mark As Answer" on that post and/or "Vote as Helpful*******

    Thursday, August 8, 2013 7:40 AM
  • You can programmatically attach a termset to a site-collection using SP 2013 Client Side Object Model.

    NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(context,
                    termSet2, context.Web, "GlobalNavigationTaxonomyProvider");

                navTermSet.IsNavigationTermSet = true;
                navTermSet.TargetUrlForChildTerms.Value = "~site/Pages/Topics.aspx";

                NavigationTerm navterm = navTermSet.CreateTerm(termname, NavigationLinkType.SimpleLink, Guid.NewGuid());
                navterm.SimpleLinkUrl = navurl;

    Refer my blog post for more details

    http://msmvps.com/blogs/sundar_narasiman/archive/2012/11/18/manipulate-taxonomy-objects-using-sharepoint-2013-client-object-model.aspx


    Please mark the replies as answers if they help or unmark if not.

    Thursday, August 8, 2013 10:39 AM