Today my co-worker asked me for help “Can you setup the search dropdown navigation for all subsites in the site collection? The search navigation should inherit from the root site.”.
Pretty simple requirement, I thought.
I started to configure the search navigation setting.

Created Search Navigation Dropdown
Issue: The dropdown displayed as expected but the sub-sites were not inheriting the search navigation from the root site of the site collection.

Searchbox Root Site
Solution: I created this powershell script to copy the navigation nodes from the root site to all subsites. Feel free to use and comment if you have any questions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
#region Parameters param( [Parameter(Mandatory = $true)] [ValidateSet('site','webapplication')] [string]$Scope, [Parameter(Mandatory = $true)] [string]$URL, [bool]$RemoveExisting = $false ) if(-not($Scope)) { Throw "You must supply a value for -Scope. Examples -Scope site or -Scope webapplication" } if(-not($URL)) { Throw "You must supply a value for -URL. Examples -URL http://mysharepoint.com" } #endregion #region Functions #Save the search navigation nodes $masterweb = Get-SPWeb $URL $masternodes = $masterweb.Navigation.SearchNav function Remove-SearchNav([string]$SiteURL) { $site = Get-SPSite $SiteURL foreach ($web in $site.AllWebs) { $SearchNav = $web.Navigation.SearchNav IF ($SearchNav -ne $NULL -and $web.url -ne $site.Rootweb.Url) { Write-Host -ForegroundColor Green "Removeing Search Navigation for" $web.url; foreach($node in $web.Navigation.SearchNav) { $web.Navigation.SearchNav.delete($node) } } $web.Dispose() } $site.Dispose() Write-Host -ForegroundColor Red "=============================================" } function Update-SearchNav([string]$SiteURL) { Write-Host -ForegroundColor Red "=============================================" Write-Host -ForegroundColor Green "Updating Search Navigation at URL " -NoNewline; Write-Host -ForegroundColor Green $SiteURL remove-varable $site = Get-SPSite $SiteURL foreach ($web in $site.AllWebs) { Write-Host -ForegroundColor Red "=============================================" Write-Host -ForegroundColor Green "Updating Search Navigation at URL " -NoNewline; Write-Host -ForegroundColor Green $web.Url $SearchNav = $web.Navigation.SearchNav IF ($SearchNav -ne $NULL) { Write-Host -ForegroundColor Red "This Site Search Navigation Already containing values"; } ELSE { foreach ($node in $masternodes) { Write-Host -ForegroundColor Green "Adding Search Navigation" $node.Title; $web.Navigation.SearchNav.AddAsLast($node) } } $web.Dispose() } $site.Dispose() Write-Host -ForegroundColor Red "=============================================" } If ($Scope -eq "webApplication") { $WebApplication = Get-SPWebapplication $URL if($RemoveExisting -eq $true) { Foreach ($Sites in $WebApplication.Sites) { Remove-SearchNav($Sites.URL) } Write-Host "Cleared the existing search navigation" } $site.Dispose(); Foreach ($Sites in $WebApplication.Sites) { Update-SearchNav($Sites.url.trim()) } } elseif ($Scope -eq "site") { if($RemoveExisting -eq $true) { Remove-SearchNav($URL) Write-Host "Cleared the existing search navigation" } Update-SearchNav($URL) } Write-Host "Press any key to continue ..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |
Hi,
You cannot call a method on a null-valued expression.
At
+ $w.Navigation.SearchNav.AddAsLast($node)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any solutions?
Regards,
John
I think this is among the most significant info for me. And i am glad reading your article. But want to remark on some general things, The website style is wonderful, the articles is really great D. Good job, cheers gcbegcdceckc
This worked wonders for me, however I now want to add a third dropdown and when I try to re-run the script to push this into the sub-sites I get:
“This Site Search Navigation Already containing values”
Is this only capable of running once? How do I update sub-sites if I update the top level?