In this post we will discuss how we can create a custom result source and then how we can use the custom result source inside a content search web part in SharePoint online.
SharePoint provides result sources by using which we can limit search results to specific content. When user performs a search, the system matches the query with a result source to provide results. SharePoint comes with many preconfigured result sources for you to use, including Conversations, Documents, and Pictures etc.
We can also create custom result sources at various scopes like:
– Service Application Level / SharePoint Admin Center for Office 365: When we create result scope at search service application level, the result source will be available to any site collection that consumes the Search Service Application.
– Site Collection: When we create result scope at the site collection level then the result source can be used by any site within the site collection.
– Site : When we create result scope at the site level then the result source can be used by that site only.
Also, you can read:
– Microsoft Flow Example Save tweets that include the specific hashtag to a SharePoint list
– Working with Field Template using JSLink in SharePoint 2013
– How to set the default value to a field in a list using Rest API in SharePoint Online?
Create Custom Result Sources:
We can create a custom result sources at the site collection level as well as site level. To create custom result sources at the site collection level go to the Site Settings and then click on “Search Result Sources” which is under “Site Collection Administration”.
And if you want to create a custom result sources at a site level then click on “Result Sources” which is under “Search”.
Here we will create a custom result source for Site level. So click on Site settings and then click on “Result Sources” which is under “Search”.
This will open the Manage Result Sources page, here click on “New Result Source” like below:
Here in this page also you can see lots of default result sources as well as custom result sources.
Here in General Information section give a Name, Description for the custom result source. Then Protocol as Local SharePoint. And Type as “SharePoint Search Results”.
Now in the Query Transform click on “Launch Query Builder” like below:
This will open the Query Builder where we can build the search query. Here we will modify the Query text like below:
When we are writing the query like below:
We are telling that we only want results from the specified path and anything under it like folders, subfolders or subsite etc.
But we will modify the query a bit to search only a particular type of file like docx file.
path: “https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MySearchTestDocLib/” FileType:docx
The FileType property instructs the query to only return files that have a .docx extension. The full query looks like below:
You can also click on the Test Query button to see the result like below, where it will display only docx files.
how to use custom result source in content search web partSimilarly, if you want to add multiple filters then you can add like below:
path: “https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MySearchTestDocLib/” FileType:docx FileType:txt FileType:pdf
Only you save the query then now Query Transform and Credential Information will look like below:
Then click on Save which will create our custom result source which we can see under Manage Result Sources page like below:
Now our result source is ready and in the next step we will see how we can use it in a content search web part.
How to use Custom Result Source inside a Content Search Web Part?
Now we will add a content search web part to a web part page. If you are new to the content search web part, then you can see a few articles below:
– Content Query Web Part and Content Search Web Part in SharePoint 2013
– SharePoint 2013 Online Content Search WebPart with Query String
– Create web part page using visual studio 2013 in SharePoint 2013
You can create a web part page in SharePoint 2013. And then edit the page and then from web part categories Content Rollup choose Content Search like below:
Once the web part added, the web part will display recently changes items. Now edit the web part properties. In the Content Search web part properties click on Change query like below:
This will open the Build Your Query window and here click on Switch to Advanced Mode.
content search web part SharePoint 2016
In the Advanced Mode, in the Select a query, you will be able to see the custom result source which we have created. Choose it and if you will click on Test Query you will be able to see the results also.
custom result source to show the only word or pdf document SharePoint 2013Click on OK and Save the Page, you can see the search results like below where it will only show word documents.
Hope this will be helpful.
Download FREE SharePoint 2019 Installation PDF Guide
This FREE PDF contains, Steps by step guide to install SharePoint 2019 Preview, prerequisites, hardware and software new features. First impression to SharePoint Server 2019 new modern site, modern lists and modern document libraries.