Metadata Navigation, the Server Side code

Posted on Updated on

Using metadata navigation in SharePoint 2013, although all problems identified and documented over the Net, is very powerful. As in all features, this one is no exception, as to be configured properly.

Narahari Patro, have a sample for creating with Powershell, i made some small modifications bellow, see original post.

This samples are based in 2 Levels, change for more, this is only to provide a sample of it, enjoy

#change these variables based on environment
$siteName = "http://yoursite.local" 
$termStoreName = "Managed Metadata Service"
$termGroupName = "For Group"
$termsetName = "Term Name"
$appendUrl = "http://yoursite.local" 
function CreateTerm( $parent, $name, $url )  
 {  
  Write-Host "Adding term $name to parent $parent.Name"  
   $term = $parent.CreateTerm("$name", 1033)  
  $term.IsAvailableForTagging = $true    
  $newUrl = $url
  if(!$url.ToLower().Contains("http"))
  {  
  # Logic to append site url to relative path to create complete url which is needed for Simple Link Navigation
    $newUrl = $appendUrl + $url
  }
  $term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $newUrl)   
  return $term  
 } 

# NAVEGAÇÃO INICIAL

$site = Get-SPSite -Identity $siteName
$session = Get-SPTaxonomySession -Site $siteName
$termStore = $session.TermStores[$termStoreName]
$group = $termstore.CreateGroup($termGroupName)
$group.Description = "Navegação Global"
$termStore.CommitAll()

Write-Host "TermGroup - $termGroupName created successfully" 

$termsetName = "Navegação Global"
$termSet = $group.CreateTermSet($termsetName,2070)
$termSet.Description = "Navigation TermSet description"
$termSet.IsAvailableForTagging = $false
$termSet.IsOpenForTermCreation = $false
$navigationSet = $group.TermSets[$termsetName]
$navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
$termStore.CommitAll()

Write-Host "TermSet - $termsetName created successfully" 

# create terms for My site

$areaTrabalhoTerms = CreateTerm $termSet "R1" "/work"
$colaboradoresTerm = CreateTerm $termSet "R2" "/users"
$empresaTerm = CreateTerm $termSet "R3" "/corp"

$termStore.CommitAll()

$termSet.CustomSortOrder = $areaTrabalhoTerms.id.ToString()+":"+$colaboradoresTerm.id.ToString()+":"+$empresaTerm.id.ToString()

# create sub terms for My site

$term1 = CreateTerm $areaTrabalhoTerms "Root1" "/work/work1"
$term2 = CreateTerm $areaTrabalhoTerms "Root2" "/work/work2"
$term3 = CreateTerm $areaTrabalhoTerms "Root3" "/work/work3"

$term4 = CreateTerm $colaboradoresTerm "Root4" "/users/users1"
$term5 = CreateTerm $colaboradoresTerm "Root5" "/users/users2"

$term7 = CreateTerm $empresaTerm "SubRoot1" "/corp/corp1/"
$term8 = CreateTerm $empresaTerm "SubRoot2" "/corp/corp2"
$term9 = CreateTerm $empresaTerm "SubRoot3" "/corp/corp3"
$term10 = CreateTerm $empresaTerm "SubRoot4" "/corp/corp4"
$term11 = CreateTerm $empresaTerm "SubRoot5" "/corp/corp5"
$term12 = CreateTerm $empresaTerm "SubRoot6" "/corp/corp6"
$term13 = CreateTerm $empresaTerm "SubRoot7" "/corp/corp7"
$term14 = CreateTerm $empresaTerm "SubRoot8" "/corp/corp8"

$areaTrabalhoTerms.CustomSortOrder = $term1.id.ToString() +":"+ 
    $term2.id.ToString() +":"+ 
    $term3.id.ToString()
$colaboradoresTerm.CustomSortOrder = $term4.id.ToString() +":"+ 
    $term5.id.ToString() +":"+ 
    $term6.id.ToString()
$empresaTerm.CustomSortOrder = $term7.id.ToString() +":"+ 
    $term8.id.ToString() +":"+ 
    $term9.id.ToString() +":"+ 
    $term10.id.ToString() +":"+ 
    $term11.id.ToString() +":"+ 
    $term12.id.ToString() +":"+ 
    $term13.id.ToString() +":"+ 
    $term14.id.ToString()
$termStore.CommitAll()

Write-Host "Terms for TermSet - $termsetName created successfully"  
 

The advantages are enormous, like you can deprecate one item without delete it, then can be reused. I made a sample with c# Server Side to read this items.

image

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            var site = SPContext.Current.Site;
            var session = new TaxonomySession(site); 
            if (session.TermStores.Count == 0) return;
            var termStore = session.TermStores["Managed Metadata Service"];
                foreach (var  group in  termStore.Groups)
                {
                    if (@group.Name != "Your Group Name") continue;
                    foreach (var termSet in  @group.TermSets)
                    {
                        if (termSet.Name != "Your Root TermSet") continue;
                        foreach (var term in termSet.Terms)
                        {
                            if (term.IsDeprecated == false)
                            {                                        
                                 Console.Write(term.Name)

                                foreach (var t in term.Terms)
                                {
                                    if (t.IsDeprecated == false)
                                    {
                                        var url = t.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"];
                                        Console.Write(url + t.GetDefaultLabel(2070) 
                                    }
                                }
                            }
                        }
                    }
                }
        });
    }
    catch (Exception ex)
    {
        //Log
    }
} 
Advertisements

2 thoughts on “Metadata Navigation, the Server Side code

    Fábio Carvalho said:
    March 26, 2015 at 19:43

    Thanks João.

    I used your code to create a custom menu and this works well with a good performance.

    For completing your post, all properties about terms:

    – _Sys_Nav_Title: the Navigation Node Title
    – _Sys_Nav_FriendlyUrlSegment: the Friendly Url, also represented by the FriendlyUrlSegment property of the NavigationTerm class.
    – _Sys_Nav_TargetUrl: the target URL, also represented by the TargetUrl property of the NavigationTermclass.
    – _Sys_Nav_TargetUrlForChildTerms: the target URL for child terms, also represented by the TargetUrlForChildTerms property of the NavigationTerm class.
    -_Sys_Nav_CatalogTargetUrl: the catalog target URL, also represented by the CatalogTargetUrl property of the NavigationTerm class.
    -_Sys_Nav_CatalogTargetUrlForChildTerms: the catalog target URL for child terms, also represented by the CatalogTargetUrlForChildTerms property of the NavigationTerm class.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s