How to change the options in search drop down with CSOM

searchNavigation1

You can change the options in the drop down manually by going to Site Settings > Search > Search Settings and add or remove a link.

searchNavigation2

If you want to change the options of the drop down in the search bar, you unfortunately have to configure these for each web and not just for the root site collection. So you probably don’t want to configure it manually.

As you can’t you SSOM in SharePoint online you have to use the CSOM. You can’t access the search navigation in a “normal” way, because there is no object in CSOM for it. But you can get the search Navigation by its id (Thanks for the idea, Mikael):


public static void AddSearchNavigationNode(Web web, string title, string url)
{
var nav = web.Navigation;
NavigationNode searchNavigation = nav.GetNodeById(1040);
NavigationNodeCollection searchNavigationNodeCollection = searchNavigation.Children;
NavigationNodeCreationInformation searchNavigationNode = new NavigationNodeCreationInformation();
searchNavigationNode.Title = title;
searchNavigationNode.Url = url;
searchNavigationNode.IsExternal = true;
searchNavigationNode.AsLastNode = true;
searchNavigationNodeCollection.Add(searchNavigationNode);
web.Context.Load(searchNavigationNodeCollection);
web.Context.ExecuteQuery();
}

Please note that all the default options (Everything, People, This site) are getting removed, when adding a custom option via code.