Web is full of articles explaining how to modify SharePoint Online SPWeb navigation settings such as
- Navigation Settings csom Powershell for SharePoint online
- https://sharepointycl.wordpress.com/2017/01/27/updating-managed-navigation-settings-for-sharepoint-online-using-powershell/
- https://github.com/SharePoint/PnP/blob/master/Components/Core.TaxonomyNavigationComponents/Utility/Navigation.ps1
But for some reason the line
$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $ctx, $web
throws me error
New-Object : Cannot find an overload for "WebNavigationSettings" and the argument count: "2".
At C:\Work\SharePoint\ModifySiteNavigationSettings.ps1:51 char:27
+ ... nSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I've tried with both SP Online Client Components SDKs 16.0.6906.1200 (latest) and 16.0.6518.1200, but error stays the same.
dotPeeking the Microsoft.SharePoint.Client.Publishing assembly contains the below, so it's not about the number of arguments.
public WebNavigationSettings(ClientRuntimeContext context, Web web)
Any tips what could be causing this and how to make it work?
Here's my full code
param(
[string]$siteUrl,
[string]$username,
[string]$password
)
$path = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\"
Add-Type -Path (Resolve-Path "$($path)Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$($path)Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$($path)Microsoft.SharePoint.Client.Taxonomy.dll")
Add-Type -Path (Resolve-Path "$($path)Microsoft.SharePoint.Client.Publishing.dll")
if (-not $siteUrl)
{
Write-Host "Specify site url in siteUrl parameter" -foregroundcolor red
return
}
if (-not $username)
{
Write-Host "Specify user name in username parameter" -foregroundcolor red
return
}
if (-not $password)
{
Write-Host "Specify user password in password parameter" -foregroundcolor red
return
}
function Inherit-Navigation-For-Web($ctx, $web, $taxSession)
{
Write-Host "Inherit navigation for" $web.Url -foregroundcolor green
$ctx.Load($web)
$web.AllProperties["__CurrentNavigationIncludeTypes"] = 0
$web.Update()
$ctx.ExecuteQuery()
$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $ctx, $web # <<< ERROR COMES HERE
$navigationSettings.CurrentNavigation.Source = [Microsoft.SharePoint.Client.Publishing.Navigation.CurrentNavigationSource]::PortalProvider
$navigationSettings.Update($taxSession)
Write-Host " Navigation is updated" -foregroundcolor green
$ctx.Load($web.Webs)
$ctx.ExecuteQuery()
$web.Webs | ForEach-Object { Inherit-Navigation-For-Web $ctx $_ $taxSession }
}
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.RequestTimeOut = 1000 * 60 * 10;
$ctx.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
$web = $ctx.Web
$ctx.Load($web)
#$ctx.Load($ctx.Site)
$ctx.ExecuteQuery()
$taxSession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctx)
Inherit-Navigation-For-Web $ctx $ctx.Web $taxSession
You execute it with
.\ModifySiteNavigationSettings.ps1 "https://tenant.sharepoint.com" "[email protected]" "password"