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.