While working for different clients, I have found that the most common requirements of all by the customers is to enable users to search only in a particular SharePoint List or document library. In this blog, I will show how you can setup your result source to target a specific list or document library. However, in this post I willn’t explain how to create custom display templates to change the look and feel of how search results should display.
Creating a Result Source to search in a list
- Go to Site Settings. Under ‘Search’, click on ‘Result Sources’.
- Click on ‘New Result Source’. Give a Name to the result source, choose the ‘Protocol’ as ‘Local SharePoint’ and ‘Type’ as ‘SharePoint Search Results’.
- Now, launch the query builder under ‘Query Transform’.
- Type the following query text –
{SearchBoxQuery} (Path:http://[siteurl]/lists/[yourlistname]) (contentclass:STS_ListItem)
{SearchBoxQuery} – This indicates the search text entered by the user
Path: – Path is the managed property that indicates the url of the sharepoint list to be searched.
contentclass: Adding STS_ListItem as contentclass instructs sharepoint search to search only in the sharepoint list forms and not in the list view pages such as ‘AllItems.aspx’ etc.
Creating a Result Source to search in a document library
- Repeat steps 1 through 3 as shown above.
- In the query builder, the query text wouls also be same as for the list except the url would change to point to the document liberary such as
{SearchBoxQuery} (Path:http://[siteurl]/[DocLibraryName]) (contentclass:STS_ListItem)
However, the query above would also search within the documents stored in the document library. To restrict the search from searching content within the documents, use the query shown below –
{SearchBoxQuery} (Path:http://[siteurl]/[DocLibraryName] FileExtension<>docx) AND (Path:http://[siteurl]/[DocLibraryName] FileExtension<>pdf) AND (contentclass:STS_ListItem)
Unfortunately, in the query text we need to explicitly mention all the file extension types that needs to be ignored while searching by adding FileExtension<> [fileextension].
Leave a Comment