Monday, January 27, 2014

SharePoint 2013 - Update Search Navigation Nodes for all Sitecollections & subwebs in your Web Application

Search navigation links represents the search result pages in SharePoint Search center. By default we have four search results pages in SharePoint search center, "Everything", "People", "Conversations", and "Videos" as shown in image below.

Those options are Shown as tabs in your search center



or as a drop down in your search box




But if you added your own result pages that are linked to specific result sources or query rules, You have to add the new pages to your search navigation settings per each webapplication

What if you have a Mulit-Site collection , Mullti-Webs Structure hierarchy … In the subweb search settings you will find that search settings have an option “Use the same results page settings as my parent”, But this will inhirit the search settings only not the search navigation links as the links are SPNavigationNode object,


So you will have to do this task manually…. Or just code it


I have written a powershell script file to update all the webs "DOWNLOAD FILE HERE", sites within your web application, In my case we have added new search results pages “Events”, “File Share” …etc…. So manipulate the URLs the titles to meet your needs

The Code:


function Update-SearchNav([string]$Identity)
{
 Write-Host -ForegroundColor Red "============================================="
 Write-Host -ForegroundColor Green "Updating Search Navigation at URL " -NoNewline;
 Write-Host -ForegroundColor Green $Identity

 $s = Get-SPSite $Identity
 $w = $s.RootWeb

 foreach ($w in $s.AllWebs) { 
  Write-Host -ForegroundColor Red "============================================="
  Write-Host -ForegroundColor Green "Updating Search Navigation at URL " -NoNewline;
  Write-Host -ForegroundColor Green $w.Url
  
  $SearchNav = $w.Navigation.SearchNav
  
  IF ($SearchNav -ne $NULL)
  {
   Write-Host -ForegroundColor Red "This Site Search Navigation Already containing values";
  }
  ELSE
  {
   Write-Host -ForegroundColor Red "Search Navigation was not found";
   
   Write-Host -ForegroundColor Green "Adding Search Navigation Everything";
   $Title = "Everything"
   $RelativeUrl = "/sites/SearchCentre/pages/results.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation Events";
   $Title = "Events"
   $RelativeUrl = "/sites/SearchCentre/Pages/events.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation People";
   $Title = "People"
   $RelativeUrl = "/sites/SearchCentre/Pages/peopleresults.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation Conversation";
   $Title = "Conversation"
   $RelativeUrl = "/sites/SearchCentre/Pages/conversationresults.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation File Share";
   $Title = "File Share"
   $RelativeUrl = "/sites/SearchCentre/Pages/FileShare.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation Videos";
   $Title = "Videos"
   $RelativeUrl = "/sites/SearchCentre/Pages/videoresults.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation This Section";
   $Title = "This Section"
   $RelativeUrl = $w.ServerRelativeUrl + "/_layouts/15/osssearchresults.aspx?u={contexturl}"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

  }
  Write-Host -ForegroundColor Red "============================================="
    } 
 
 $w.Dispose()
 $s.Dispose()
 Write-Host -ForegroundColor Red "============================================="
}

#TODO Add Your Web Application URL

$WebApplication = Get-SPWebApplication http://webapplicationurl


Foreach ($Sites in $WebApplication.Sites)
{ 
 Update-SearchNav($Sites.url.trim())
 Write-Host "Press any key to continue ..."

 $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

}

7 comments:

  1. When a subweb has no navigation nodes $w.Navigation.SearchNav is null and you can't add lastnode

    ReplyDelete
    Replies
    1. I tried this PowerShell on a new SiteCollections with no Search Navigation Nodes and it works fine, Please let me know what was the scenario you tried this PS on

      Delete
  2. If there are existing navigation nodes, how can delete them and replace them with new entries?

    ReplyDelete
    Replies
    1. http://blogs.microsoft.co.il/choroshin/2014/05/11/sharepoint-2013-add-and-remove-items-from-search-navigation-using-powershell/

      Delete
  3. Hi Islam,

    Could you please let us know how to delete a node as per 'Anonymous' comment above.

    Thanks you
    Lena

    ReplyDelete
    Replies
    1. Hello - PLease check this link http://blogs.microsoft.co.il/choroshin/2014/05/11/sharepoint-2013-add-and-remove-items-from-search-navigation-using-powershell/

      Delete
  4. I removed some nodes and updated URLs and it worked like a champ.Thanks for sharing.

    ReplyDelete