Monday, December 7, 2015

SharePoint 2013 Result Sources: Include Content Only From The Current Site, NOT Sub Sites

From the title of this blog article you have probably guessed what I will discuss, how to actually only search on the current site!  Many blogs and articles have suggested that Path:{Site} or Path:{Site.URL} is the correct approach to limiting the results to the current site, but this solution is incorrect and only addresses part of the issue.

For those of you who don’t want the journey or explanation, the key to your success is to include this as part of your query:


Why is The Path Solution Incorrect?
The Path solution is incorrect since Path:{Site} will include any content from your sub site(s) that match the query criteria.  The reason for this is that the child sites include the path of the parent site, which means you will always get the data from the sub sites which inherit from the parent’s Path.  You may be thinking well Dan, why don’t you just use Path={Site} instead of contains.  If you have tried this syntax on its own, you most likely only received the Home Page of the site from which the query was issued from.  Content within the site obviously does not have the URL of the homepage, so using equals will only return the one page which matches the path exactly.  In short, you can use Path:{Site} when you are trying to exclude contents from parent sites, but include content from the current site and it’s children.

How to Return Results ONLY From the Current Site:
To only obtain results from the site from which the query is being run, WebId={Site.ID} should be used.  WebId is an out of the box managed property which houses the site’s GUID/ID, so essentially the query only return items where the WebId is equal to the current site’s GUID/ID.

Simple Example Query:
SPContentType=”Item” AND WebId={Site.ID}

This query will only retrieve items which match the item content type display name and where the WebId is equal to the current site’s GUID/ID.  If you were to try this same query from a top level site utilizing Path:{Site} instead of WebId like the other blogs and sites suggest, you will retrieve Items with this content type from all of the sub sites beneath this path.  The query above will ONLY return items from the current site!

Best of Luck!