CSOM powershell to update search navigation sharepoint 2013
-
01-10-2020 - |
Question
I am wanting to write some client side powershell to update the search navigation on global search. I have found many examples of this with server side but none using client side. anyone with any ideas?
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web ServerExtensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
function New-SearchNavigation()
{
param(
[Parameter(Mandatory=$true)]$siteurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials
)
try
{
##############################################################################
function AddSearchLink([string]$searchsiteUrl, [string] $navTitle,[string] $navUrl){
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
$ctx.Credentials = $credentials
$rootWeb = $ctx.Web
$sites = $rootWeb.Webs
$ctx.Load($rootWeb)
$ctx.Load($sites)
$ctx.ExecuteQuery()
foreach($searchweb in $sites)
{
$ctx.Load($searchweb)
$ctx.ExecuteQuery()
if ($searchweb.title -contains "search" ){
Write-Host $searchweb.Title "-" $searchweb.Url
$node=New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @($navTitle,$navUrl,$true);
$searchweb.Navigation.AddToSearchNav($node);
$searchweb.Update()
}
}
#$searchweb=Get-SPWeb $searchsiteUrl
##DeleteSearchLink -searchsiteUrl $searchsiteUrl -navTitle $navTitle
}
AddSearchLink("mysite/search", "TEST", "/search/test.aspx")
}
catch
{
Write-Host ("Error while creating search result page. Error -->> " + $_.Exception.Message) -ForegroundColor Red
Write-Output("Error while creating search result page. Error -->> " + $_.Exception.Message)
}
}
$credentials = Get-Credential
$url = mysite
New-SearchNavigation $url $credentials
I get an error saying cannot find type [Microsoft.SharePoint.Navigation.SPNavigationNode]:
Solution
Try adding the type Microsoft.SharePoint.Client.NavigationNode
and then switching Microsoft.SharePoint.Navigation.SPNavigationNode
to Microsoft.SharePoint.Client.NavigationNode
SPNavigationNode
is the server object model, to use the client object model, you want NavigationNode
. See here for details.
However, unlike SPNavigationNode
, NavigationNode
does not take a constructor with two arguments, see here for NavigationNode and here for SPNavigationNode.