SharePoint 2013 – Add and Remove Link Items From Search Navigation Using PowerShell

May 11, 2014

Here’s a quick tip on how to add link items to SharePoint 2013 search navigation and how to remove items from search navigation.

Add Items:

$sites=Get-SPsite http://testsite -Limit all | Get-SPweb
$node1 = new-object  -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode"  -ArgumentList "Link1", "http://site/sites/Search/Pages/pageA.aspx", $true
$node2 = new-object  -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode"  -ArgumentList "Link2", "http://site/sites/Search/Pages/PageB.aspx", $true
$node3 = new-object  -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode"  -ArgumentList "Link3", "http://site/sites/Search/Pages/PageC.aspx", $true
foreach ($web in $sites)
{
 $web.Navigation.SearchNav.AddAsFirst($node1)
 $web.Navigation.SearchNav.AddAsFirst($node2)
 $web.Navigation.SearchNav.AddAsFirst($node3)
}

Remove items:

$navItems=@();
$sites=Get-SPsite http://testsite -Limit all | Get-SPweb
foreach($web in $sites){

$navs = $web.Navigation.SearchNav
   foreach ($nav in $navs)
   {
    $nodeid =$web.Navigation.GetNodeById($nav.Id)
    $idItems+=$nodeid
    write-host "add id $($nodeid.Id) to array"  
   }   
   foreach ($node in $navItems)
   {
    $web.Navigation.SearchNav.delete($node)
    write-host "delete id $($node.Id)"    
   }     
  $web.update()   
}
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

2 comments

  1. cogmiosMarch 14, 2015 ב 0:45

    shouldnt it be $idItems+=$nodeid >> $navitems+=$nodeid ?

    Reply
    1. BobCApril 29, 2015 ב 15:52

      @cogmios is right, there is a type-o in the delete script.

      Change this line $idItems+=$nodeid

      to $navitems+=$nodeid

      -Bob

      Reply