1

My requirement was to delete Conversations vertical from the default SharePoint search verticals(Everything, People, Conversations, This site) and Default vertical was set to This site.

Default Search

But once I added(Everything, People, This site) using CSOM , the default vertical has been set to Everything(the first vertical).

Search box after adding verticals using CSOM

Is there any way by which I can set the default vertical again to This site using CSOM without changing the order of verticals ??

Below is the example of what I trying to achieve :

  1. We have 4 verticals (Everything, People, Conversations, This Site) by default and This Site is last on the drop down But still text displayed in the search box is "Search this site" (as above first image).
  2. After deleting Conversations vertical, text displayed in the search box is "Search Everything" or "Search People" (as above first image) whichever is the first on the drop down.

I want to display "Search This site" even after deleting the Conversations vertical and with the same order(Everything, People, This site) which is not happening.

I hope this is more clear now.

1

With CSOM, SP.NavigationNode object does not have the property for specifying the order r of the node.

As a workaround, we can use CSOM to re-create search navigation links. The SP.NavigationNodeCreationInformation object describes a new navigation node to be created. And it has properties we can use to specify the older:

enter image description here

CSOM Demo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using Microsoft.SharePoint.Client;

namespace SPOSearchNav
{
    class Program
    {
        private class Configuration
        {
            public static string ServiceSiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>";
            public static string ServiceUserName = "<admin>@<tenant>.onmicrosoft.com";
            public static string ServicePassword = "<password>";
        }

        static ClientContext GetonlineContext()
        {
            var securePassword = new SecureString();
            foreach (char c in Configuration.ServicePassword)
            {
                securePassword.AppendChar(c);
            }
            var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
            var context = new ClientContext(Configuration.ServiceSiteUrl);
            context.Credentials = onlineCredentials;
            return context;
        }  

        static void Main(string[] args)
        {
            var clientContext = GetonlineContext();  
            Web web = clientContext.Web;

            // Get the search navigation node collection.
            NavigationNode searchNav = web.Navigation.GetNodeById(1040);
            NavigationNodeCollection nodeCollection = searchNav.Children;
            clientContext.Load(nodeCollection);
            clientContext.ExecuteQuery();

            //delete all node in search navigation
            nodeCollection.ToList().ForEach(node => node.DeleteObject());
            clientContext.ExecuteQuery();

            // create
            // Set properties for a new navigation node.
            NavigationNodeCreationInformation node1 = new NavigationNodeCreationInformation();
            node1.Title = "This Site";
            node1.Url = "/sites/<site>/_layouts/15/osssearchresults.aspx?u={contexturl}";
            node1.AsLastNode = true; //be created as the last node in the collection
            nodeCollection.Add(node1);
            clientContext.Load(nodeCollection);
            clientContext.ExecuteQuery();

            NavigationNodeCreationInformation node2 = new NavigationNodeCreationInformation();
            node2.Title = "Everything";
            node2.Url = "https://<tenant>.sharepoint.com/search/Pages/results.aspx";
            node2.IsExternal = true;
            node2.AsLastNode = true; //be created as the last node in the collection
            //node2.PreviousNode = nodeCollection[0]; //be created after the first node in the navigation node collection
            nodeCollection.Add(node2);
            clientContext.Load(nodeCollection);
            clientContext.ExecuteQuery();

            NavigationNodeCreationInformation node3 = new NavigationNodeCreationInformation();
            node3.Title = "People";
            node3.Url = "https://<tenant>.sharepoint.com/search/Pages/peopleresults.aspx";
            node3.IsExternal = true;
            node3.AsLastNode = true; //be created as the last node in the collection
            //node3.PreviousNode = nodeCollection[1]; //be created after the second node in the navigation node collection
            nodeCollection.Add(node3);
            clientContext.Load(nodeCollection);
            clientContext.ExecuteQuery();    

            foreach (NavigationNode navNode in nodeCollection)
            { Console.WriteLine(navNode.Title);}

        }
    }
}
  • Thanks for looking into it but the solution you provided does not work in my case. I want the same order(Everything, People, This site) to be displayed on the drop down while the default vertical to be searched should be "This site". With this code it will change the order (This site, Everything, People)on drop down which I don't want to do. – Ravi Aug 31 '18 at 9:46
  • what do you mean "the same order"? – JoannaW_MSFT Aug 31 '18 at 9:57
  • I meant I want to add verticals in order (Everything, People, This site) while displaying "Search This site" in the search box which is not happening. And with the code you provided, it's adding "This site" as first vertical. Added example in my question as well. I hope it's helpful. – Ravi Aug 31 '18 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.