Delen via


Specify Default Scope for Advanced Search Box Web Part without Using Scope Picker

On a recent project I had a requirement to develop a search application for a specific subset of content within SharePoint. By creating managed properties for the site columns of my content type and then consuming them as properties in the advanced search box web part I was able to create a usable search interface quickly without writing custom code. An issue arose, however, when I wanted to limit the search results to a particular scope.

Apparently when a scope is not specified via the scope picker the advanced search box web part uses the “All Sites” scope thus my results included matching content outside of my specific scope. To meet my requirement I needed to find a way to specify the default scope for an advanced search box query without input from a user. One way to do this would have been to create a custom web part that called the search API via the object model and then I would have complete control over the user interface. I always counsel my clients, however, to use the out of the box functionality within SharePoint whenever possible and felt that a custom web part for such a minor issue was overly complex.

My first idea to solve this was to create a new scopes display group containing only the scope I needed and then use this display group to populate the scope picker list of the advanced search box web part. This was an effective solution but had a significant drawback in that it still required a user to specify the scope. Next I went out to my internet search provider of choice and found that most developers had resorted to javascript that selected the checkbox for the scope picker automatically when a search was performed if the user failed to specify a scope. I quickly decided this was not an ideal solution and continued searching.

This led me to investigate the advanced search box web part further and the solution presented itself when I went and viewed the XML for the “Properties” property of the web part. Within the <ResultType> schema there is a node called <Query>. It is here that the out of the box configuration specifies the various result types used to bind the result type picker to. Below is the default results type XML for the advanced search box web part.

   <ResultTypes>
    <ResultType DisplayName="All Results" Name="default">
      <Query/>
      <PropertyRef Name="Author" />
      <PropertyRef Name="Description" />
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
    </ResultType>
    <ResultType DisplayName="Documents" Name="documents">
      <Query>IsDocument=1</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
    <ResultType DisplayName="Word Documents" Name="worddocuments">
      <Query>FileExtension='doc' Or FileExtension='docx' Or FileExtension='dot'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
    <ResultType DisplayName="Excel Documents" Name="exceldocuments">
      <Query>FileExtension='xls' Or FileExtension='xlsx' Or FileExtension='xlt'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
    <ResultType DisplayName="Presentations" Name="presentations">
      <Query>FileExtension='ppt'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
  </ResultTypes>

You’ll notice that within the <Query> node that query syntax is being used to filter the search as opposed to keyword syntax. This is important and may explain why you see little to no documentation on how to specify a scope in an advanced search query. In order to specify a scope in query syntax you must enclose the scope restriction in quotes as follows: “scope”=’Your Scope’.

Queries for the “Advanced Search Box” web part, however, are declared in XML. Therefore, in order to enclose the scope restriction in quotes you must escape the quotes in order to make them XML safe. You do this by using the entity reference of &quot;. Other properties do not require this, only the scope property. Below is an example of limiting the out of the box result types for the advanced search box web part to a specific scope.

   <ResultTypes>
    <ResultType DisplayName="All Results" Name="default">
      <Query>&quot;scope&quot;='Your Scope'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="Description" />
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
    </ResultType>
    <ResultType DisplayName="Documents" Name="documents">
      <Query>IsDocument=1 And &quot;scope&quot;='Your Scope'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
    <ResultType DisplayName="Word Documents" Name="worddocuments">
      <Query>FileExtension='doc' Or FileExtension='docx' Or FileExtension='dot' And &quot;scope&quot;='Your Scope'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
    <ResultType DisplayName="Excel Documents" Name="exceldocuments">
      <Query>FileExtension='xls' Or FileExtension='xlsx' Or FileExtension='xlt' And &quot;scope&quot;='Your Scope'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
    <ResultType DisplayName="Presentations" Name="presentations">
      <Query>FileExtension='ppt' And &quot;scope&quot;='Your Scope'</Query>
      <PropertyRef Name="Author" />
      <PropertyRef Name="DocComments"/>
      <PropertyRef Name="Description" />
      <PropertyRef Name="DocKeywords"/>
      <PropertyRef Name="FileName" />
      <PropertyRef Name="Size" />
      <PropertyRef Name="DocSubject"/>
      <PropertyRef Name="Path" />
      <PropertyRef Name="Created" />
      <PropertyRef Name="Write" />
      <PropertyRef Name="CreatedBy" />
      <PropertyRef Name="ModifiedBy" />
      <PropertyRef Name="Title"/>
      <PropertyRef Name="Manager" />
      <PropertyRef Name="Company"/>
    </ResultType>
  </ResultTypes>

Now that you have declared a scope in your result type query your advanced searches will be limited to the specified scope. This means the scope picker does not need to be displayed as user input is no longer required to limit your results to a particular scope and you can do this without one line of custom code or client side script. I hope you will find this useful and consider using the advanced search box web part for your search applications in SharePoint.

Comments

  • Anonymous
    June 02, 2010
    The comment has been removed

  • Anonymous
    September 10, 2010
    Hi, thanks for this. After much experimenting, I eventually found that the Search Core Results webpart allows you to specify a search scope, which means that you can specify a scope without having to use the scope picker or customise the search properties. Best Wishes, Jeff

  • Anonymous
    September 11, 2010
    Matt, Thank you for the information.  Being a newbie I need just a little more guidance.  In the 'Your Scope' what goes here? I have tried everything, I think: ...  'This Site: Site Title' ...  'This Site Site Title' ...  'This Site' ...  and a few other attempts that were completely unsuccessful Help!!!!!

  • Anonymous
    October 17, 2010
    Hi, mattbremer    Your method is useful, but when I set it with this method, I get into trouble.    I need to select others in the Result type first, then select the All Results. In this steps, I will get the correct result, otherwise, it will list the result of the scope All Results. What can I do? Please help me, thank you!    My msn is:limei_zhejiang@live.cn. Please help me, thank you!

  • Anonymous
    November 02, 2010
    Hi, This is great, Coming to my requirement, one of our users require the following customization to be done with the Properties drop down list, I was able to manage removing the items and add my properties, but the user wants the item "Name" to be default selection in the drop down in the "Add property restrictions..." section. As the sharepoint displays (Pick property). Is there any possibility of defaulting it to Name instead of (Pick property)

  • Anonymous
    December 12, 2010
    I also would like to understand what needs to go into 'Your Scope'. Ideally I want it to be something like "This site", without having to specify a specifi site name since I want to be able to use this advanced search page for all my site collections.

  • Anonymous
    February 25, 2011
    Sorry for my late response to your comments. It has been a while since I checked in on my blog. To answer the value for 'Your Scope' questions, you need to input whatever the name of your scope is. Whether it is a shared scope defined in Central Administration or a site collection specific scope defined in Site Settings. So for instance by default you have a scope called 'All Sites', that would be a value for this property. I hope that helps. Thanks.

  • Anonymous
    September 14, 2011
    Thanks a lot. It saved my work...

  • Anonymous
    December 26, 2011
    Thank you so very much.This helped me when I most needed it !!!! Highly obliged...

  • Anonymous
    February 02, 2012
    If anyone has arrived here and looking to achieve the same thing in SharePoint 2010 then do the same as described in the blog above but instead of adding it into the <Query> tag it must be the <KeywordQuery> tag and change the content inside of the tag to simply look like this (replacing the "Your Scope" with the name of your actual scope) Scope:"Your Scope" So the whole tag should look like: <KeywordQuery>Scope:"Your Scope"</KeywordQuery> Hope that helps! Rich

  • Anonymous
    April 16, 2012
    Thanks, got it working.  I needed to put the block of ORs in parenthesis then it started to work.  Great post!

  • Anonymous
    June 12, 2012
    The scope does not get passed in the search for the first resultType  <ResultType DisplayName="All Results" Name="default">      <Query>"scope"='Your Scope'</Query> if i change the order of the resultType ,then which ever is the first one does not take the scope restriction in the search. I am using Sharepoint 2010 and tried with <KeywordQuery> as well ,but the same problem exists there as well.

  • Anonymous
    October 03, 2012
    How can we get result if we select more than one scope? or an we change checkbox to radio button using OOB configuration ?

  • Anonymous
    November 28, 2012
    The comment has been removed

  • Anonymous
    November 28, 2012
    Following my prior comment, It turned out I had a typo in the XML. However, even with that fixed, the method described on this page does not allow a custom search scope to be passed for SP2010, it only allows for a custom search field value, which is no the goal. With SP2010, the scope is defined using two URL modifiers, "CS=" which sets the scope to something like "This Site" or whatever scope you have defined, and the "U=" which sets the URL to that site. There is currently no way I have found to embed those two values via the XML set or to set them in the path to the search page itself.

  • Anonymous
    October 03, 2014
    The comment has been removed

  • Anonymous
    February 25, 2015
    The comment has been removed

  • Anonymous
    April 23, 2015
    The comment has been removed