How to: Provision SharePoint 2013 Search Dropdowns Programatically

In my last post, we looked at how you can provision some of the search properties for SharePoint 2013 programatically. In this post we’ll see how you can get search drop downs provisioned programatically.

Essentially here is how the search drop downs look like:

Dropdowns Programatically

What does it give you?

Well, your users can select what they want to search and based on that selection you can redirect them to an appropriate search results page which handles searching of a particular content type etc. In my example above, if the user selected News, the search results page that will respond will only display results with that keyword from news content type.

In this post we’ll only discuss how to add drop down programatically. You will need to create your own search results pages.

I’ll be using SharePoint 2013 Feature (site Feature) with a feature receiver, but you can port the code to a console application etc.
The feature will have to run on the root site collection – not the search site.
Also, I assume you already have a search site collection with URL : /search. This is very important because if you don’t have the search site created – things in code will return an exception.

Here is the code for the feature receiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
string searchSiteUrl = “search”;
string resultPage = “/search/Pages/results.aspx”;

if (site != null)
{
site.RootWeb.AllProperties[“SRCH_ENH_FTR_URL_SITE”] = string.IsNullOrEmpty(searchSiteUrl) ? null : searchSiteUrl;
site.RootWeb.AllProperties[“SRCH_ENH_FTR_URL”] = string.IsNullOrEmpty(searchSiteUrl) ? null : searchSiteUrl;

string srchSite = ModifySharedSearchSettings(site.RootWeb.GetProperty(“SRCH_SB_SET_SITE”) as string, resultPage);
if (!string.IsNullOrEmpty(srchSite))
{
site.RootWeb.SetProperty(“SRCH_SB_SET_SITE”, srchSite);
}

string srchWeb = ModifySharedSearchSettings(site.RootWeb.GetProperty(“SRCH_SB_SET_WEB”) as string, resultPage);
if (!string.IsNullOrEmpty(srchWeb))
{
site.RootWeb.SetProperty(“SRCH_SB_SET_WEB”, srchWeb);
}
}
}

private string ModifySharedSearchSettings(string srchSite, string resultPage)
{
SharedSearchBoxSettings settings = SharedSearchBoxSettings.Deserialize(srchSite) ?? new SharedSearchBoxSettings();
settings.Inherit = false;
settings.ResultsPageAddress = resultPage;
settings.ShowNavigation = true;
return settings.Serialize();

}

Now this was only the first part of the code which sets the settings of the search on the root site collection and the root web.
The second part is creation of the actual drop down options, you can run it in the same receiver:

SPNavigationNode AllContentNode = new SPNavigationNode(“All Content”, “/search/Pages/results.aspx”, true);
site.RootWeb.Navigation.SearchNav.AddAsFirst(AllContentNode);

SPNavigationNode Newsnode = new SPNavigationNode(“News”, “/search/Pages/newsresults.aspx”, true);
site.RootWeb.Navigation.SearchNav.AddAsFirst(Newsnode);

SPNavigationNode Formsnode = new SPNavigationNode(“Forms”, “/search/Pages/formsresults.aspx”, true);
site.RootWeb.Navigation.SearchNav.AddAsFirst(Formsnode);

site.RootWeb.Update();

Also, i noticed that if the specific results page doesn’t exist – the option will not be added. So before you run this code above, either created all the newsresults.aspx, and formsresults.aspx or set the same results.aspx for all options.

Enjoy!

Check out Yaroslav Pentsarskyy blog ShareMuch for more insightfull content.

For more expert information on search check out the free eBook ‘The Essential Guide to Enterprise Search in SharePoint 2013‘ by Jeff Fried, Agnes Molnar, Michael Himelstein, Tony Malandain & Eric Moore. Download Now>>

Share this on...

Rate this Post:

Share: