Adding a link on Quick Launch with PowerShell
-
07-01-2021 - |
Question
I have a PowerShell script that creates an Office 365 group and configures the associated SharePoint site. During the creation process it tries to add a link on the site's quick launch using the following code
$ctx = Get-PnPContext;
$finalizeLinkAdded = $false;
try {
$web = $ctx.Web
$navColl = $web.Navigation.QuickLaunch
$ctx.Load($navColl) ;
$ctx.ExecuteQuery();
$newNavNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$newNavNode.Title = "Link Title"
$newNavNode.Url = $link
$newNavNode.AsLastNode = $true
$navColl.Add($newNavNode) | out-null;
$web.Update();
$ctx.ExecuteQuery();
$finalizeLinkAdded = $true;
}
catch {
write-host "$((get-date).toString()) Error adding navigation link $($_.toString())";
}
This works well when I'm runnig the script from a PowerShell console. However, this script is run by creating a job
$job = Start-Job -InitializationScript { d:\powershell\engagements\assemblies.ps1}
-ScriptBlock { d:\powershell\create-group.ps1 }
When I do it this way, I get this strange error.
Cannot convert argument "parameters", with value:
"Microsoft.SharePoint.Client.NavigationNodeCreationInformation", for "Add" to type
"Microsoft.SharePoint.Client.NavigationNodeCreationInformation":
"Cannot convert the "Microsoft.SharePoint.Client.NavigationNodeCreationInformation"
value of type "Microsoft.SharePoint.Client.NavigationNodeCreationInformation" to
type "Microsoft.SharePoint.Client.NavigationNodeCreationInformation"."
I don't understand why this doesn't work when the script is run within a start-job scriptblock. What am I doing wrong?
La solution
I am not sure of why do you have error but i would like to suggest you an alternative that i have been using. Since i already see you using PnPContext you should use Add-PnPNavigationNode try adding below syntax and adjust as per your need. for more information refer to MSDN page
$w = Get-PnPWeb
$lurl = $w.Url + "/Lists/Contactpersonen"
Add-PnPNavigationNode -Title "Contactpersonen" -Location "QuickLaunch" -Web $w -Url $lurl