How can I limit search results to only one child site below the current site?
-
29-09-2020 - |
Question
I have an application where the end user is able to add new subsites to existing sites, and add sub sites to those sub sites and so on. As part of the site template I have a Search Results Web Part which is supposed to return a list of sites (and information from a custom SiteInformation list located on that site) one level below the current one i.e. show the site information for this sites immediate children.
Unfortunately using a simple query like
Path:"{\Site.URL}" ContentTypeID:MySiteInfoContentType
shows results from every site below the current site including all child and sub sites.
How can I restrict the search to only return results from one level of sub sites down?
I've been trying to do this using a wildcard search with space for one level of child site i.e.
Path:"{\Site.URL}" + "/*/Lists/SiteInformation/DispForm.aspx"
But it's not getting me anywhere. Is there a better way to limit search results by depth of sub site? For example is there a Site Parent variable which I could use?
Many thanks.
Solution
You can try using the property UrlDepth
. It allows you to manipulate how deep the URL needs to go for the query. for example;
UrlDepth=1 returns things like http://site/subsite
UrlDepth=2 returns things like http://site/pages/page.aspx or http://site/subsite/subsite
May help a bit.
OTHER TIPS
In SharePoint 2013 probably the easiest way to restrict search query to a specific web would be to specify WebId
property:
WebId:"Web Id goes here"
Example
How to determine WebId property for a web site
The following REST query returns SP.Web.ID
property:
https://[site collection]/[web]/_api/web?$select=Id
Here's what worked for me;
path:"https://contoso/sites/programs" UrlDepth=3 contentclass:STS_Web Site<>{Site.URL}
with some trickery from ShareGate team https://www.youtube.com/watch?v=KAaYKe9HaDU
As Matt says, you can you the UrlDepth param, however, remember to use it right after the path param, in order to make it work accordingly.
Here is an example, that will return only the 1st level child sites of a given site:
path:{site url} UrlDepth:3 contentclass:STS_Web