Thursday, October 2, 2014

Caution when limiting results for a site collection

One way to limit results to the current site collection in for example a result source or search web part is to use the following query template:

{searchTerms} SiteID:{SiteCollection.ID}

Turns out that there is a bug in SharePoint on this (present in both SPO and on-prem at the time of writing) where the ID returned for the SiteCollection is the ID of the root web, not the ID of the site collection itself. As per the MSDN documentation it should return the GUID of the site collection where the query was issued.

The workaround is to use the URL instead of the ID.

{searchTerms} SPSiteURL:{SiteCollection}