Skip to main content

SharePoint 2013 and "search scopes"

While SharePoint 2013 introduced some undeniably handy features in the content discovery department, some of the old functionality has changed so much that you'll have to forget many of the design tricks that worked in 2007 and 2010.  One of such pain points is deprecating search scopes in SharePoint 2013.

The requirement

Let's start with a trivial situation. You have a requirement that states:

"A user should be able to perform different types of search from any page/sub site on the portal by selecting a type of search and specifying keyword(s)".

This would be considered an out-of-box functionality in 2007/2010: you just enable search scopes dropdown (considering the search box control is already in the master page), and define scopes. Piece of cake! Well, not so much in 2013.

 

The problem

The thing is - there's no search scopes in SharePoint 2013. OK, to be exact - there's no user-definable search scopes, as we still have a couple of built-in locked-down scopes left for some backward compatibility, but that does not help us at all. What do we have? Per Microsoft:

The search scope functionality was replaced by a more powerful functionality for result sources.

Result Sources! When you take a closer look at them, you'll find out they are indeed much more powerful than search scopes. Some very complex query scenarios are now fairly easy to configure. Cool. Now let's see why the result sources do not exactly work for our initial requirement.
  1. Unlike with the search scopes, you cannot specify a result source as a query string parameter for your search results page. A result source must be pre-assigned as a parameter to whatever search results web part you are using. That generally means you have to create a results page per result source you're planning on using. That's right, if you have some customizations on your results page, you'll have to duplicate those customizations on every single results page. Not to mention admin overhead to keep those pages in synch content-wise. OK, that's somewhat inconvenient, but doable.
  2. Unlike the search scopes, the result sources do not appear in the search dropdown. At least not directly.
So, what does appear in the search dropdown? Is there such a thing at all? Yes, there is, though it's visually integrated with the search box itself (a rather good UX decision, I'd say):



If those are not scopes, and not result sources - what are they? Technically speaking, those are links to the search results pages (though they can be anything, even the external links). How do you control them? Through this nice and handy Search Navigation management UI:



"So, this is it" - you would say - "we configure result sources, create result pages for them and add links to the Search Navigation". Yes, that would've worked if we did not have a requirement of having those dropdown items on EVERY sub site. The thing is - Search Navigation is site-scoped, not site collection or else. Don’t' get too excited about that "Use the same results page settings as my parent" checkbox - it will only get the parent's result page settings, but not the search navigation items. So, if you select this checkbox, and specify no search navigation items, you will get the default Everything/People/Conversations/This Site regardless of what you've got in the parent. Of course, you can duplicate search navigation settings in all sub sites, but if you have a dozen of them, and a dozen of sub sites, plus frequently created new sub sites - your site administrators will hate you passionately.
 

Microsoft solution

What does Microsoft say about it? Didn't they think of it? Of course they did. And they have a solution. At least what they consider being one:
  1. Deploy a Search Center site;
  2. Configure search navigation in Search Center;
  3. Specify search center in site collection/search service web app, and have sub sites inherit the search settings.
Neat, isn't it? Well, let's see what we're going to get here:
  • Those of you who ever had to customize Search Center already do not like this solution. Yes, that "special" master page that you will have to create and maintain separately. Yes, that lack of global navigation support. You know the drill.
  • The user experience is terrible. Let's say you're on some portal page, searching for an office.
    • What you want:
      • In the global search box type in name, and select "Offices";
      • Click "Search";
      • Get the results in a format specific to "Offices" search.
    • What you get:
      • In the global search box type in name;
      • Click "Search";
      • Get the results from a default result source (that might not even contain your type of search results), in some generic format (that might not even be suitable for displaying your type of results);
      • Select the "Offices" search type;
      • Get the results in a format specific to "Offices" search.

I can't think of any UX professional that would approve such implementation. This is where you would probably start thinking of implementing your own custom Search Box control, that looks into some central place for those "scopes". Well, don't do that just yet.
 

Our solution

Why not just write a simple timer job that synchronizes search navigation of every sub-site with, say a root of site collection? For every sub site you just synchronize the following two properties:

Search settings:

SPWeb.AllProperties["SRCH_SB_SET_WEB"]

- it's just a string that most likely looks something like

{\"Inherit\":false,\"ResultsPageAddress\":null,\"ShowNavigation\":true}
 

Search navigation:

SPWeb.Navigation.SearchNav 

- this is an SPNavigationNodeCollection object that cannot be assigned, so you'll have to cycle through it adding each node:


foreach (SPNavigationNode node in nodes)
{
    if (web.Navigation.GetNodeByUrl(node.Url) == null)
    {
        web.Navigation.AddToSearchNav(node);
    }
}


Don't forget to delete the removed search navigation nodes as well, and call the SPWeb.Update() a the end!


To summarize, in order to fully comply with our requirement, we need to:
  1. Create result sources;
  2. Create a page with configured search results web part per result source;
  3. Create a timer job that synchronizes sub site search settings with those of site collection root ones;
  4. Create search navigation entries in the site collection root, and see them appear globally throughout the site collection.
Hope, this helps you find some peace with UX designers and SharePoint administrators...

Comments

  1. If all you wanted to do was change the default from the built-in to everything instead of this site you could override the search template (see http://www.eliostruyf.com/replacing-the-ootb-small-search-input-box-for-sharepoint-2013/) and put a property in to set the default scope 1001 where 1001 is the Everything scope.

    ReplyDelete
    Replies
    1. It erased my xml but the property would be Name="DefaultDropdownNodeId"

      Delete
  2. Hi Jason,

    No, I needed to literally resemble the global search scopes functionality which was deprecated in 2013, but still requested by the clients. The default search box UX was not the issue, and worked just fine.

    On the other hand, you have reminded me of a slightly different approach to this problem, where I could create a custom search box control that looks up result sources in the root web. That way we don't need a timer job. However, the total amount of custom code would've been larger, as I would've spent time re-creating the OOB functionality, like search suggestions.

    ReplyDelete
  3. One possible solution might be to extend the existing search box and hide the NavigationNodes member. You'd have to inherit from "Microsoft.Office.Server.Search.WebControls.SearchBoxScriptWebPart" and create a getter like "public new[] NavigationNodes { get {} }".

    This requires very little custom code and you get all the goodies of the OOTB part such as user preferences, search suggestions etc.

    ReplyDelete
    Replies
    1. Hi Pranav,

      Yes, this is a possibility. The problem with this approach is using the OOTB site templates with default master pages - they will contain reference to the original control.

      Delete
  4. Thank you for this solution, Sergey. The problem for me is that my Site Collection Administration Site Settings screen has all of the same fields as your screenshot, but it does not have the Configure Search Navigation section - which is exactly the section I need to make the changes my company is requesting. Can someone please help me with this?

    ReplyDelete
    Replies
    1. Hi Aaron,

      You are most likely looking at the Site Settings -> Site Collection Administration -> Search Settings, and not the Site Settings -> Search -> Search Settings.

      Delete
  5. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly about microsoft sharepoint tutorial, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..

    ReplyDelete
  6. Thanks for sharing such a wonderful Post with us. I learnt alot from your post. I am appreciating from you to you will share more information about it. Please keep sharing. Thanks Alot

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Navigation Settings in SharePoint 2013 Site Definitions

Who likes creating SharePoint site definitions from scratch? Well, it's not my favorite part of the SharePoint development either. An initially good idea of setting up your whole site through an XML file, received implementation and support far from being perfect. That quickly lead developers to look for alternative ways of defining and provisioning sites, primarily through the SharePoint API. A simple ONET.XML with beefy code-packed features became de-facto standard. What was supposed to be a template became compiled code. I was hoping SharePoint 2013 would at least attempt to do something about it… Tasked with creating a few publishing portal site definitions, I was unpleasantly surprised to find out that any attempt to use my new site definition to provision a site collection root results in both Global and Current Navigation switched to "Managed Navigation" mode, while I needed it to stay in the good old "Structural Navigation" one. This mode is new to Sh

Distributed Cache and SharePoint 2013

We all know that in version 15 SharePoint is now integrated with AppFabric's distributed cache. There's a Distributed Cache service running on each SharePoint server (with exception of the database ones), AppFabric cache is now configured by SharePoint installation routine, and finally there is a new namespace in SharePoint API: Microsoft.SharePoint.DistributedCaching.Utilities.   You could definitely use AppFabric's cache with SharePoint 2010, as long as you create a wrapper class that takes care of cache configuration, cluster and endpoints instances, etc. However, now that the AppFabric's cache is integrated, you would probably expect something as simple as SPDistributedCache.Get(cacheKey). Well, even a quick look at the poorly-documented Microsoft.SharePoint.DistributedCaching.Utilities  classes tells you this is still far from being true. Essentially the only benefit you currently get from this API is ability to access the distributed cache configuration, and us