2

Like described in the title, i create some scope. On my document library, i've an advanced search webpart and i'd like to add my created scope. Do you have an easy way to do this?

Thanks

  • you're wanting to do this in C#? – jpollar Feb 24 '15 at 21:11
1

Here's an example of how to do it in C#

SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy
(SPServiceContext.GetContext(SPContext.Current.Site));

KeywordQuery query = new KeywordQuery(proxy);
query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;

query.HiddenConstraints = "scope:\"MyScope\"";

Full example: http://nikpatel.net/2013/02/21/step-by-step-building-custom-sharepoint-2010-search-applications-using-keywordquery-api/

| improve this answer | |
1

You can specify the Search Scope in XML of Advance Search Web part.

  • Export the Advanced Search Web part
  • Open the web part file in a text editor
  • You will notice something like this

<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>

You’ll notice that within the 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 ". 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.

Edit the file as below to include the 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>

Now that you have declared a scope in your result type query your advanced searches will be limited to the specified scope.

  • Save the file and upload webpart
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.