Thursday, February 12, 2009

Search Scopes & Contextual Search Scopes

By Sveta Yerpilev - SharePoint Technical Consultant
KWizCom Professional Services – http://www.kwizcom.com/

Summary
Part 2 of 3 part SharePoint Search Customization series .

*****************

Search Scopes:
You can crawl all items under a certain URL and choose to specify search scopes for your users i.e when they search it will only be in the areas you defined.
For example: You have a site http://server/ and sub-sites of this site, http://server/subsite1 and http://server/subsite1. You crawled all of http://server/ including the sub-sites but in the search scopes you can create a special scope for http://server/subsite1 and another scope for the http://server/subsite1.
You can create scopes on the server level (which will be applied on all Web Applications). You can also define scopes in a Site Collection.
To create a new scope - open the Scopes page in the Search Settings page or the Search Scopes page into the Site settings under the root site of a Site Collection. You can set a Scope name, add the Scope to display groups (you can connect a display group to the search box and give your users the option to search only in specified scopes) and then set the target results page.

After you create a scope you need to add a rule or rules to this scope.
Like crawl rules, here you can include or exclude items or sites but you have an additional option: “Required”. This option directs the search scope to require the rule query from every item in the scope. So if you add all content to the scope but the “Document” content type is required you will see only documents in the search result.
You can create rule that looks for a specific property. To add additional properties to this option you need to open the Metadata Property Mappings and find your property.



After that just switch the “Allow this property to be used in scopes” checkbox to “Yes”.

If you don’t find the property you are looking for in the list you need to add it to the list.
You can define that only site columns become a managed property. To do so, go to “Crawled Properties” and find your property (column) in the list (if it’s a site column it will be under the SharePoint folder).
Enter the property and verify that the “Include in Index” checkbox is checked. If not, check this option and run Incremental Crawl to see this property in your scope. Afterwards go once more to the "Metadata Property Mappings” page and create a new managed property.
Add mapping to the crawled property you created before and allow this property to be used in scopes. Now you can use this property in all search scopes.

Contextual Search Scope :
Sometime you will see a scope named “This Site” or “This List”. These names signify that within the search scope there is a contextual search scope added by SharePoint.
This is a useful option when you want to enable your users to search only in the area they are working in at the time of their search (why waste precious time crawling other locations if you know that what you are searching for is somewhere within your current location?).


If you want to remove an existing contextual scope you may find this post useful:
http://littletalk.wordpress.com/2008/10/23/removing-contextual-this-sites-cope-from-simple-search-box/



If you like and use this function and you customized your search results page you may encounter some problems. You can’t really tell the contextual search scope to send a query to a customized results page. Contextual search scope always uses a default results page - _layouts/osssearchresults.aspx.
You can customize this page but if you have more than one SharePoint Portal the correct thing to do is to redirect your user to the appropriate, customized search results page. When the user types a search query and presses “Go” the search page redirects the query to the results page using URL parameters. In the address bar of the results page you can see parameters like “k”(search query) and “u”(URL). You need to redirect users from osssearchresults.asx page to customized results page using the same parameters. The "u” parameter can be indicator, pointing to the site from which the search query originated.
Below is code that will help you perform the redirection:

function getParam( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else return results[1];
}

function redirectTo()
{
urlTmp=getParam("u");
if(urlTmp.indexOf("site1url")>0)
{
window.location="http://
customresultspageurl.aspx?k="+getParam("k")+"&cs="+getParam("cs")+"&u="+getParam("u");
}
}

redirectTo();

Just replace the “site1url” and “customresultspageurl” with your values and put this code into the osssearchresults.asx.

Coming next week:
Part 3 of the 3 part SharePoint Search Customization series: "Search Results" where we will discuss customization of your search results page.

1 comment:

Pangloss14 said...

Are you aware of any simple way to enable the user, after forwarding his/her results the custom results page (which presumably is a tab created from the search with tabs template), to maintain searching on that context?

Just so I'm clear:

1. Searching This Site: Home
2. Redirect to Search Center: HomeResults.aspx tab.

3. Modify the search terms so that I am still searching on Home.

Thanks.