Configure Manage metadata Top Navigation in SharePoint Online using PowerShell/CSOM
Steps to Create a Top Navigation using Taxonomy.
Create a CSV file with below columns with sample data.
Use the below PowerShell/CSOM script to read the data from CSV file and create term set in Manage metadata navigation.
#Configure meta data navigation
function ConfigureGlobalNavigation()
{
begin{
try
{
Write-Host "Configure a top navigation menu..." -ForegroundColor Green
Add-Content -Path $LogFileName -Value([string]::Concat("Configure a top navigation menu..."))
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)
$context.Load($MMS)
$context.ExecuteQuery()
#Retrieve Term Stores
$TermStores = $MMS.TermStores
$context.Load($TermStores)
$context.ExecuteQuery()
#Bind to Term Store
$TermStore = $TermStores[0]
$context.Load($TermStore)
$context.ExecuteQuery()
$TermStoreId=$TermStore.Id
#Bind to Group
#Update the Group name https://contoso.com/_layouts/15/termstoremanager.aspx
$Group = $TermStore.Groups.GetByName("ContosoSiteCollection")
$context.Load($Group)
$context.ExecuteQuery()
#Bind to Term Set
$TermSets = $group.TermSets
$context.Load($TermSets)
$context.ExecuteQuery()
$TermSet = $TermSets | Where-Object {$_.Name -eq $SelectedNavigationName}
if($TermSet)
{
Write-Host $SelectedNavigationName "Top navigation menu already configured: " $SelectedNavigationName -foregroundcolor red
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation menu already configured", $SelectedNavigationName))
$TermSet = $group.TermSets.GetByName($SelectedNavigationName)
$context.Load($TermSet)
$context.ExecuteQuery()
}
else
{
$TermSet = $Group.CreateTermSet($SelectedNavigationName, [System.Guid]::NewGuid(), $TermStore.DefaultLanguage)
$TermSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
$TermSet.CustomSortOrder=$true
$context.load($TermSet)
$context.ExecuteQuery()
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation group is created: ", $SelectedNavigationName))
$TermSetId=$TermSet.Id
#$CSVGlobalNavigationFilePath - Update the CSV file path
$L1Terms = Import-CSV $CSVGlobalNavigationFilePath
Foreach ($row in $L1Terms)
{
if(($row.Level2Title.Trim().Length -eq 0) -and ($row.Level3Title.Trim().Length -eq 0) )
{
$Level1TitleAdd = $TermSet.CreateTerm(($row.Level1Title),1033,[System.Guid]::NewGuid().toString())
if($row.Level1Title -eq "Home")
{
$Level1TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl","/"+$SubSiteURL.Split("/")[3]+"/"+$SubSiteURL.Split("/")[4]+"/"+$SubSiteURL.Split("/")[5])
}
else
{
$Level1TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $row.URL)
}
$context.Load($Level1TitleAdd)
$context.ExecuteQuery()
$GlobalNavigationTermsGuids+=$Level1TitleAdd.Id.ToString()+":"
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation L1 Title Created: ", $row.Level1Title))
}
if(($row.Level2Title.Trim().Length -gt 0) -and ($row.Level3Title.Trim().Length -eq 0))
{
$Level2TitleAdd = $Level1TitleAdd.CreateTerm(($row.Level2Title),1033,[System.Guid]::NewGuid().toString())
$Level2TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $row.URL)
$context.Load($Level2TitleAdd)
$context.ExecuteQuery()
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation L2 Title Created: ", $row.Level2Title))
}
if(($row.Level1Title.Trim().Length -gt 0) -and ($row.Level2Title.Trim().Length -gt 0) -and ($row.Level3Title.Trim().Length -gt 0))
{
$Level3TitleAdd = $Level2TitleAdd.CreateTerm(($row.Level3Title),1033,[System.Guid]::NewGuid().toString())
$Level3TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $row.URL)
$context.Load($Level3TitleAdd)
$context.ExecuteQuery()
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation L3 Title Created: ", $row.Level3Title))
}
}
$TermSet.CustomSortOrder=$GlobalNavigationTermsGuids;
$context.Load($TermSet)
$context.ExecuteQuery()
#set both current and global navigation settings to managed
$taxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)
$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $context, $context.Web
#set both current and global navigation settings to managed
$navigationSettings.CurrentNavigation.Source = "taxonomyProvider"
$navigationSettings.CurrentNavigation.TermStoreId=$TermStoreId
$navigationSettings.CurrentNavigation.TermSetId=$TermSetId
$navigationSettings.GlobalNavigation.Source = "taxonomyProvider"
$navigationSettings.GlobalNavigation.TermStoreId=$TermStoreId
$navigationSettings.GlobalNavigation.TermSetId=$TermSetId
$navigationSettings.Update($taxonomySession)
$context.ExecuteQuery()
Write-Host "Top navigation menu configuration completed" -foregroundcolor green
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation menu configuration completed"))
}
}
catch
{
Write-Host $_.Exception.Message -foregroundcolor Red
Add-Content -Path $LogFileName -Value([string]::Concat("Configure Global Navigation :",$_.Exception.Message))
}
}
end {
Write-Host "Top navigation menu configuration completed" -foregroundcolor green
}
}
Cls
#Specify tenant admin and site URL
$User = "Naga@contoso.com"
$SiteURL = "https://*****.sharepoint.com/"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
if (!$Context.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$SiteURL'" -ForegroundColor Green
}
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
ConfigureGlobalNavigation
Happy coding..
Steps to Create a Top Navigation using Taxonomy.
Create a CSV file with below columns with sample data.
Use the below PowerShell/CSOM script to read the data from CSV file and create term set in Manage metadata navigation.
#Configure meta data navigation
function ConfigureGlobalNavigation()
{
begin{
try
{
Write-Host "Configure a top navigation menu..." -ForegroundColor Green
Add-Content -Path $LogFileName -Value([string]::Concat("Configure a top navigation menu..."))
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)
$context.Load($MMS)
$context.ExecuteQuery()
#Retrieve Term Stores
$TermStores = $MMS.TermStores
$context.Load($TermStores)
$context.ExecuteQuery()
#Bind to Term Store
$TermStore = $TermStores[0]
$context.Load($TermStore)
$context.ExecuteQuery()
$TermStoreId=$TermStore.Id
#Bind to Group
#Update the Group name https://contoso.com/_layouts/15/termstoremanager.aspx
$Group = $TermStore.Groups.GetByName("ContosoSiteCollection")
$context.Load($Group)
$context.ExecuteQuery()
#Bind to Term Set
$TermSets = $group.TermSets
$context.Load($TermSets)
$context.ExecuteQuery()
$TermSet = $TermSets | Where-Object {$_.Name -eq $SelectedNavigationName}
if($TermSet)
{
Write-Host $SelectedNavigationName "Top navigation menu already configured: " $SelectedNavigationName -foregroundcolor red
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation menu already configured", $SelectedNavigationName))
$TermSet = $group.TermSets.GetByName($SelectedNavigationName)
$context.Load($TermSet)
$context.ExecuteQuery()
}
else
{
$TermSet = $Group.CreateTermSet($SelectedNavigationName, [System.Guid]::NewGuid(), $TermStore.DefaultLanguage)
$TermSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
$TermSet.CustomSortOrder=$true
$context.load($TermSet)
$context.ExecuteQuery()
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation group is created: ", $SelectedNavigationName))
$TermSetId=$TermSet.Id
#$CSVGlobalNavigationFilePath - Update the CSV file path
$L1Terms = Import-CSV $CSVGlobalNavigationFilePath
Foreach ($row in $L1Terms)
{
if(($row.Level2Title.Trim().Length -eq 0) -and ($row.Level3Title.Trim().Length -eq 0) )
{
$Level1TitleAdd = $TermSet.CreateTerm(($row.Level1Title),1033,[System.Guid]::NewGuid().toString())
if($row.Level1Title -eq "Home")
{
$Level1TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl","/"+$SubSiteURL.Split("/")[3]+"/"+$SubSiteURL.Split("/")[4]+"/"+$SubSiteURL.Split("/")[5])
}
else
{
$Level1TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $row.URL)
}
$context.Load($Level1TitleAdd)
$context.ExecuteQuery()
$GlobalNavigationTermsGuids+=$Level1TitleAdd.Id.ToString()+":"
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation L1 Title Created: ", $row.Level1Title))
}
if(($row.Level2Title.Trim().Length -gt 0) -and ($row.Level3Title.Trim().Length -eq 0))
{
$Level2TitleAdd = $Level1TitleAdd.CreateTerm(($row.Level2Title),1033,[System.Guid]::NewGuid().toString())
$Level2TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $row.URL)
$context.Load($Level2TitleAdd)
$context.ExecuteQuery()
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation L2 Title Created: ", $row.Level2Title))
}
if(($row.Level1Title.Trim().Length -gt 0) -and ($row.Level2Title.Trim().Length -gt 0) -and ($row.Level3Title.Trim().Length -gt 0))
{
$Level3TitleAdd = $Level2TitleAdd.CreateTerm(($row.Level3Title),1033,[System.Guid]::NewGuid().toString())
$Level3TitleAdd.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $row.URL)
$context.Load($Level3TitleAdd)
$context.ExecuteQuery()
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation L3 Title Created: ", $row.Level3Title))
}
}
$TermSet.CustomSortOrder=$GlobalNavigationTermsGuids;
$context.Load($TermSet)
$context.ExecuteQuery()
#set both current and global navigation settings to managed
$taxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)
$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $context, $context.Web
#set both current and global navigation settings to managed
$navigationSettings.CurrentNavigation.Source = "taxonomyProvider"
$navigationSettings.CurrentNavigation.TermStoreId=$TermStoreId
$navigationSettings.CurrentNavigation.TermSetId=$TermSetId
$navigationSettings.GlobalNavigation.Source = "taxonomyProvider"
$navigationSettings.GlobalNavigation.TermStoreId=$TermStoreId
$navigationSettings.GlobalNavigation.TermSetId=$TermSetId
$navigationSettings.Update($taxonomySession)
$context.ExecuteQuery()
Write-Host "Top navigation menu configuration completed" -foregroundcolor green
Add-Content -Path $LogFileName -Value([string]::Concat("Top navigation menu configuration completed"))
}
}
catch
{
Write-Host $_.Exception.Message -foregroundcolor Red
Add-Content -Path $LogFileName -Value([string]::Concat("Configure Global Navigation :",$_.Exception.Message))
}
}
end {
Write-Host "Top navigation menu configuration completed" -foregroundcolor green
}
}
Cls
#Specify tenant admin and site URL
$User = "Naga@contoso.com"
$SiteURL = "https://*****.sharepoint.com/"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
if (!$Context.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$SiteURL'" -ForegroundColor Green
}
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
ConfigureGlobalNavigation
Happy coding..
No comments:
Post a Comment