1

Web is full of articles explaining how to modify SharePoint Online SPWeb navigation settings such as

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" "username@tenant.onmicrosoft.com" "password"
1

It was conflict with v15 assemblies, although I explicitly did Add-Type from 16 hive. Deleting Client assemblies from 15 hive's ISAPI folder fixed the issue.

|improve this answer|||||
1

You missed a comma, get coffee :)

Simply modify the line as below, should work :

$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $ctx, $web

Update: Looks like there is some issue.Try it as below:

$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $ctx, $web 
$navigationSettings.CurrentNavigation.Source = "portalProvider"
$navigationSettings.Update($taxSession)

or

$navigationSettings = New-Object Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings $ctx, $web 
$navigationSettings.CurrentNavigation.Source = [Microsoft.SharePoint.Client.Publishing.Navigation.StandardNavigationSource]::PortalProvider
$navigationSettings.Update($taxSession)
|improve this answer|||||
  • Thanks, will get yet another cup :D However, I originally had another error, so the missing comma (and thus error) was just a result of me troubleshooting. Updated the question with the original error that comes even when I have comma. – Jussi Palo Oct 11 '17 at 8:01
  • Just saw your answer , glad that it worked :) . I tried your code however it throws error at the line $navigationSettings.CurrentNavigation.Source = [Microsoft.SharePoint.Client.Publishing.Navigation.CurrentNavigationSource]::PortalProvider, so have modified it. I have used the latest CSOM dlls as well. – Gautam Sheth Oct 11 '17 at 8:59
  • You're correct, it needed to be [Microsoft.SharePoint.Client.Publishing.Navigation.StandardNa‌​vigationSource]::Por‌​talProvider] instead of [Microsoft.SharePoint.Client.Publishing.Navigation.CurrentNa‌​vigationSource]::Por‌​talProvider], one other remnant from troubleshooting. – Jussi Palo Oct 11 '17 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.