Tuesday, December 20, 2016

Inherit global and current navigation settings on the Sharepoint publishing sites from parent web via client object model

Sometimes on Sharepoint publishing site we need to inherit global or current navigation settings from parent site:

In order to do that via client object model the following PowerShell script can be utilized:

   1:  
   2: param(
   3:     [string]$siteUrl,
   4:     [string]$username,
   5:     [string]$password
   6: )
   7:  
   8: $currentDir = Convert-Path(Get-Location)
   9: $dllsDir = resolve-path($currentDir + "\dlls_16_csom_16.1.3912.1204")
  10:  
  11: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  12: "Microsoft.SharePoint.Client.Runtime.dll"))
  13: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  14: "Microsoft.SharePoint.Client.dll"))
  15: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  16: "Microsoft.SharePoint.Client.Publishing.dll"))
  17:  
  18: if (-not $siteUrl)
  19: {
  20:     Write-Host "Specify site url in siteUrl parameter" -foregroundcolor red
  21:     return
  22: }
  23:  
  24: if (-not $username)
  25: {
  26:     Write-Host "Specify user name in username parameter" -foregroundcolor red
  27:     return
  28: }
  29:  
  30: if (-not $password)
  31: {
  32:     Write-Host "Specify user password in password parameter" -foregroundcolor red
  33:     return
  34: }
  35:  
  36: function Inherit-Navigation-For-Web($ctx, $web, $taxSession)
  37: {
  38:     Write-Host "Inherit navigation for" $web.Url -foregroundcolor green
  39:     $ctx.Load($web)
  40:     $ctx.ExecuteQuery()
  41:     
  42:     if ($web.ID -eq $ctx.Site.RootWeb.ID)
  43:     {
  44:         Write-Host "    Skip root web" -foregroundcolor yellow
  45:     }
  46:     else
  47:     {
  48:         $navigationSettings = New-Object Microsoft.SharePoint.Client
  49: .Publishing.Navigation.WebNavigationSettings($ctx, $web)
  50:        
  51:         $navigationSettings.GlovalNavigation.Source = [Microsoft.SharePoint
  52: Publishing.Navigation.StandardNavigationSource]::InheritFromParentWeb 
  53:         $navigationSettings.CurrentNavigation.Source = [Microsoft.SharePoint
  54: .Client.Publishing.Navigation.StandardNavigationSource]::InheritFromParentWeb
  55:         $navigationSettings.Update($taxSession)
  56:         
  57:         Write-Host "    Navigation is updated" -foregroundcolor green
  58:     }
  59:     
  60:     $ctx.Load($web.Webs)
  61:     $ctx.ExecuteQuery()
  62:     $web.Webs | ForEach-Object { Inherit-Navigation-For-Web $ctx $_ $taxSession }
  63: }
  64:  
  65: $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
  66: $ctx.RequestTimeOut = 1000 * 60 * 10;
  67: $ctx.AuthenticationMode =
  68: [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
  69: $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
  70: $credentials = New-Object Microsoft.SharePoint.Client
  71: .SharePointOnlineCredentials($username, $securePassword)
  72: $ctx.Credentials = $credentials
  73: $ctx.Load($ctx.Web)
  74: $ctx.Load($ctx.Site)
  75: $ctx.Load($ctx.Site.RootWeb)
  76: $ctx.ExecuteQuery()
  77:  
  78: $taxSession =
  79: [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctx)
  80:  
  81: Inherit-Navigation-For-Web $ctx $ctx.Web $taxSession
Script recursively goes through all sub sites in Sharepoint Online site collection and inherits their navigation settings from parent via Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings class.

No comments:

Post a Comment