Trouble with ossearchresult, droppdown box and scopes?

Have you tried to create custom scopes in a SSP ? Guess you expected them to show up in your newly created sites as well, right? Unfortunately, they don't. And you might even run into some more trouble along the way. But let's explore in an ordered fashion:

  1. Create a portal with a search center.
  2. Create a team sub-web.
  3. Create another team site collection (not a subweb).
  4. Enable "Standard sharepoint site collection features" -feature, on top level of both site collections.
  5. Create and compile one or more new scopes in your SSP.
    image

The new scopes you now created are now available in both site collections. But how can you enable the dropdown search box to include them and be available in every new site, point to your search center and still have local search (This site) pointing to a local search page?

First of all you need to create a feature so you can change the search box to your liking. That's easy enough. (remember you just used a delegate! :)

If you activate the new feature on your portal, add your new scopes (site settings - search scopes - search dropdown - check boxes). Your scopes are propagated to all subsites as expected. But not to your team site collection.

Now try and set the search settings in the team site collection.

image

Point it to your search center. That will enable scopes and let you still search locally. You can also add the new scopes manually as you did on the search center. Works great! Let's move on to automating this.

The plan is:

  1. Create one feature with feature actication eventreciever:
    1. Set the connection to the search site
    2. Add scopes to the "search dropdown" list
  2. Staple the feature to the site defs.

This actually was a bit of pain. The search settings was not easily found. It turned out it was RootWeb.AllProperties with a strange name: SRCH_ENH_FTR_URL.

Here is the code:

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

(...)

using (SPSite spSite = (SPSite)properties.Feature.Parent)
                {
using (SPWeb spWeb = spSite.RootWeb)
                    {
// Set the link to the search center
                       spWeb.AllProperties["SRCH_ENH_FTR_URL"] = properties.Feature.Properties["SearchCenterUrl"].Value;

                       spWeb.Update();

                    }
                }

(...)

 

//update the search scopes for the dropdown
ServerContext serverctx = ServerContext.GetContext(properties.Feature.Properties["SSPName"].Value);
SearchContext searchctx = SearchContext.GetContext(serverctx);

Scopes scopes = new Scopes(searchctx);
Uri uri = new Uri(((SPSite)properties.Feature.Parent).Url);
ScopeDisplayGroup group = scopes.GetDisplayGroup(uri, "Search Dropdown");
                group.Clear();
string[] eScopes = properties.Feature.Properties["Scopes"].Value.Split(';');
foreach (string arg in eScopes)
                {
Scope newScope = scopes.GetSharedScope(arg);

if(!group.Contains(newScope))
                    {
                        group.Add(newScope);
                        group.Update();
                    }
                }

Be careful here. The scopes must exist otherwise the feature will fail - add error handling. Consider also what you would like to take place in the deactivation part.

 

Let me know if you run into anything else unexpected.

 

Tip: add a width element in the ossearchresults.aspx, if you want to left align or center the search box. For some reason the web control keeps overrunning all other values.

Ref:

https://msdn2.microsoft.com/en-us/library/bb428856.aspx