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")

}

Sunday, January 26, 2014

Custom Sitemap provider in SharePoint does not show n-level :@

Another genius SharePoint  limitation or issue , If you want to implement your own custom site map provider, To implement your own Sitemap provider on SharePoint you will have to inherit from the following class :
  • -          Microsoft.SharePoint.Publishing.Navigation. PortalSiteMapProvider
  • -          Then you will have to override the following method
    • public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
  • -          After implementing your custom logic with a static or recursive algorithm (In my case was a recursive algorithm to get all navigation terms from Managed Metadata Service in a specific term set), You will find that only 2 levels are showing in the top navigation menu ….weiiiiird
  • -          Ok google it… you will find that this maybe an issue in the <SharePoint:Aspmenu> control in your masterpage which contains a property called MaximumDynamicDisplayLevels,,, OOOOH this maybe my life saver…. :@ but no SharePoint will not make your life that easy.
  • -          Playing around with this property and other properties like StaticDisplayLevels but in vain .
  • -          So what is happening in the background … Time to reflect some code
  • -          Reflecting the  Microsoft.SharePoint.Publishing.Navigation. PortalSiteMapProvider class … Here you will have following  surprise



-          Yes you are seeing it right, Microsoft engineers for some UNKNOWN reason had decided to HARDCODE the MaximumSupportedNodeDepth as a PROTECTED property to be UNACCESSABLE .
-          Now implement your logic but this time inherit for the classic System.Web.StaticSiteMapProvider implement the following methods:
o   public override SiteMapNode BuildSiteMap()
o   protected override SiteMapNode GetRootNodeCore()
-          You now have your own customized top navigation site map provider supporting n-level hierarchy


-          Happy SharePointing :)