SharePoint Online: Create term sets and apply as Global Navigation in Sites using PowerShell

This PowerShell SharePoint tutorial, we will discuss, how to create a term sets in SharePoint Online Office 365 using PowerShell. Also, we will check how to apply the term-sets as Global Navigation in SharePoint Online Site.

We always think of having automation solutions while deploying components in office 365 projects. I can also say it’s a really challenging moving list, libraries, all out of the box development from Dev to other sites.

Using PowerShell and CSOM Combination, we can write a script to automate the deployment process. I have implemented a few of the scripts to automate the deployment process, which I will be explaining in further articles.

Let’s start with “How to connect Office 365 site from PowerShell editor?”

We have to prepare our machine to write the script. Here is an interesting part you can write the scripts in SharePoint (On-Premise) server or we can use our local system to write the script.

Open Windows PowerShell ISE with Run as administrator.

If you get below error with respect to Machine policy then we just need to change the Execution policy of the system

Update Execution Policy as per below command but you need administrator access to run this command. Open the PowerShell editor as an administrator and then update the execution policy.

PS C:> Set-ExecutionPolicy Unrestricted

As said earlier I am writing the scripts in my local system, so I need to keep all SharePoint DLL’s in the local folder to provide the reference in script.

You can copy the DLL’s from the SharePoint server or download it from this URL.

I have created the Library folder to keep all required.DLL files, which I’m referring in the code.

The below code will create Term sets based on provided XML inputs.

Create term sets using PowerShell in SharePoint Online

Below is the PowerShell script which will create a term sets in SharePoint Online.

cls
$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $("$dp0\Inputs.xml")
$xmldata = [xml](Get-Content($xmlFilePath));
$username = "[email protected]"
$password = Read-Host -Prompt "Please enter your password" -AsSecureString
$url = $xmldata.WebSite.Url
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
# connect/authenticate to SharePoint Online and get ClientContext object..
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
Add-Type -Path "C:\Library\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Library\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Library\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Library\Microsoft.SharePoint.Client.Taxonomy.dll"

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: ‘$Url'" -ForegroundColor Green
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
}

function createTerms([Microsoft.SharePoint.Client.Web] $web)
{
$TMS =[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($clientContext)
$clientContext.Load($TMS)
$clientContext.ExecuteQuery()
if(!$TMS.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Taxonomy" -ForegroundColor Green
#Retrieve Term Stores
$TermStores = $TMS.TermStores
$clientContext.Load($TermStores)
$clientContext.ExecuteQuery()
if(!$TermStores.ServerObjectIsNull.Value)
{
Write-Host "Get SharePoint Taxonomy TermStores" -ForegroundColor Green
#Bind to Term Store
$TermStore = $TermStores[0]
$clientContext.Load($TermStore)
$clientContext.ExecuteQuery()
#Get Groups
$Groups = $TermStore.Groups
$clientContext.Load($Groups)
$clientContext.ExecuteQuery()
#Get Site collection Group
$Group = $Groups[2]
$clientContext.Load($Group)
$clientContext.ExecuteQuery()
if(!$Group.ServerObjectIsNull.Value)
{
Foreach ($TermMain in $xmldata.WebSite.TermMain)
{
$TermSet = $Group.CreateTermSet($xmldata.WebSite.TermMain.TermName,[System.Guid]::NewGuid().toString(),1033)
$TermSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
#$TermSet.SetCustomProperty("_Sys_Nav_CustomSortOrder", "True")
$clientContext.Load($TermSet)
$clientContext.ExecuteQuery()
Foreach ($TermSub1 in $TermMain.TermSet)
{
$TermSubSet1 = $TermSet.CreateTerm($TermSub1.Name,1033,[System.Guid]::NewGuid().toString())
$TermSubSet1.SetCustomProperty("_Sys_Nav_IsNavigationTermSet","True")
$TermSubSet1.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl",$TermSub1.Url)
$clientContext.Load($TermSubSet1)
$clientContext.ExecuteQuery()
Foreach ($TermSub2 in $TermSub1.Term)
{
$TermSubSet2 = $TermSubSet1.CreateTerm($TermSub2.Name,1033,[System.Guid]::NewGuid().toString())
$TermSubSet2.SetCustomProperty("_Sys_Nav_IsNavigationTermSet","True")
$TermSubSet2.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl",$TermSub2.Url)
$clientContext.Load($TermSubSet2)
$clientContext.ExecuteQuery()
Foreach ($TermSub3 in $TermSub2.TermSub)
{
$TermSubSet3 = $TermSubSet2.CreateTerm($TermSub3.Name,1033,[System.Guid]::NewGuid().toString())
$TermSubSet3.SetCustomProperty("_Sys_Nav_IsNavigationTermSet","True")
$TermSubSet3.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl",$TermSub3.Url)
$clientContext.Load($TermSubSet3)
$clientContext.ExecuteQuery()
}
}
}
}
}
}
}
}

createTerms $web

So here i am calling the createTerms function.

XML Input Values:

<?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>

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

Now, we will discuss, how to apply term-sets as Global Navigation in SharePoint site 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 PowerShell script code which will apply the term-sets as Global Navigation in SharePoint Online site.

cls

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

$username = "[email protected]"
$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>

You may like following SharePoint tutorials:

This SharePoint tutorial, we learned how to create term sets using PowerShell in SharePoint Online. Also, we checked how to apply term sets as Global Navigation in SharePoint site using PowerShell and CSOM.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>