Thursday, October 16, 2014

Retrieving and working with Search Navigation entries using CSOM

I’m currently working on some provisioning tasks for a search page and we want to script up the search navigation using CSOM. This would be the Everything, People, and Social tabs you see below the search box.
The issue is that Web.Navigation does not have the SearchNav property which SPWeb.Navigation has. It only has QuickLaunch and TopNavigationBar.
But just because the good guys over at Microsoft haven’t gotten around to implement all the SSOM properties doesn’t mean we’re stuck. Using Reflector I figured out the node id for the search navigation was 0x410. Which yields the following code.
string siteUrl = "http://dev/sites/search";

ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;

var nav = web.Navigation;

NavigationNode searchNav = nav.GetNodeById(1040);
NavigationNodeCollection nodeCollection = searchNav.Children;


foreach (NavigationNode navigationNode in nodeCollection)
    Console.WriteLine(navigationNode.Title + " : " + navigationNode.Url);

Take a look at for sample code on how to add more nodes to the collection.



  1. Hi Mikael, Do you know if there is any way of adding a new search navigation entry under the search settings? I followed the link on MSDN above unfortunatley it only talks about quicklaunch. I some how tried to add one but i get the following exception 'Cannot open "/search/Pages/MyCustomSearch.aspx": no such file or folder.' Do you know if there is a way around it? Thanks Vee

  2. Hi Mikael, is there any way in updating the search navigation settings so it can update the search box dropdowns. I get an error 'Cannot open : /search/pages/MyCustomSearch.aspx no such file or folder'. The file exists and I still get the exception. Do you know if there is any is any way out of this? Thank you, Vee

    1. The dropdowns are search scopes, and you can configure them somewhat on the sites search settings page. But very quirky and hard to figure out.

    2. Bit late to the party but probably just need to add the absolute path rather than the relative one.
      e.g. not /search/pages/customResults.aspx

    3. Forgot to add, if you set .IsExternal to false, then the Url resolution SharePoint performs is skipped. Not sure if that's the right approach but it works.

    Faced "Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null" exception clientContext.Load(nodeCollection) after the execute query clientContext.ExecuteQuery().
    We faced this exception only in the Community site template.If we put thread.sleep 2 minutes and load the nodecollection and execute its worked.
    Please help me on this.

    With Regards,
    Rajendhiran M

    1. On a new site, or on existing one.. and not sure why this happens.

    2. Thanks for your reply. We faced this issue community sites only for new and old while update the search navigation scope.