Apply term sets as Global Navigation in SharePoint site using PowerShell and CSOM


In this post, I am going to explain how to apply term-sets as Global Navigation in SharePoint site using PowerShell and CSOM. In my previous post, I have explained how to create term sets using PowerShell and CSOM.

Now below script has details on how to apply the term-sets as Global Navigation in SharePoint Site.

Below is the Script Code:

cls

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “emailid@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}
function UpdateTopNavWebProperty([Microsoft.SharePoint.Client.Web] $web)
{

Write-Host “set global navigation settings to Managed Metadata started” -ForegroundColor Green
$taxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($clientContext)
$clientContext.Load($taxonomySession)
$clientContext.ExecuteQuery()
$termStores = $taxonomySession.TermStores

$clientContext.Load($termStores)
$clientContext.ExecuteQuery()
try {

$termStore = $termStores[0]
$clientContext.Load($termStore)
$Groups = $termStore.Groups
$clientContext.Load($Groups)

$clientContext.ExecuteQuery()

#Get Site collection Group
foreach($Group in $Groups)
{
$clientContext.Load($Group)
$clientContext.ExecuteQuery()
if($Group.Name.Contains($xmldata.WebSite.TermMain.GroupURL))
{
$solenisTermSets=$Group.TermSets
$clientContext.Load($solenisTermSets)
$clientContext.ExecuteQuery()
foreach($solenisTermSet in $solenisTermSets)
{
$clientContext.Load($solenisTermSet)

$clientContext.ExecuteQuery()
if($solenisTermSet.Name.Contains($xmldata.WebSite.TermMain.TermName))
{
$TermID=$solenisTermSet.Id
}
}
}
}
}
catch
{
Write-Host “Error detail ” $_.Exception.Message -foregroundcolor black -backgroundcolor Red
return
}
$fId = [GUID]$TermID

$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $clientContext, $clientContext.Web

#For Display the same navigation items as the parent site
$navigationSettings.GlobalNavigation.Source = “taxonomyProvider”
$navigationSettings.GlobalNavigation.TermStoreId = $termStore.Id
$navigationSettings.GlobalNavigation.TermSetId = $fId
$navigationSettings.Update($taxonomySession)

try {
$clientContext.ExecuteQuery()
Write-Host “setting global navigation settings to Managed Metadata Completed” -foregroundcolor black -backgroundcolor green
}
catch
{
Write-Host “Error while setting global navigation settings to Managed Metadata” $_.Exception.Message -foregroundcolor black -backgroundcolor Red

}
}
UpdateTopNavWebProperty $web

Xml Inputs –

<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://company.sharepoint.com/sites/Sample” >
<TermMain TermName=”Custom Top Navigation” GroupURL=”company.sharepoint.com” >
<TermSet Name=”Home” Url=”/” Order=”1″>
</TermSet>
<TermSet Name=”Departments” Url=”/sites/Library” Order=”2″>
<Term Name=”Accounting” Url=”/sites/Accounting”>
<TermSub Name=”Accounting1″ Url=”/sites/Accounting/Accounting1″ />
</Term>
<Term Name=”IT” Url=”/sites/IT” />
<Term Name=”Marketing” Url=”/sites/Marketing” />
</TermSet>
</TermMain>
</WebSite>

Read some SharePoint 2013 tutorials:


Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →