Search query to find pages in current site and all sub-sites [SharePoint]

I was given a challenge to develop a mechanism to list all pages in the current site and its sub-sites. Why not do it using Content By Search Web Part was my initial thought. In the end I managed to achieve this, and the prototype looks like below.

1 Prototype


One of the most satisfying things in this exercise was coming up with the query to return the right search results, which is:

SPSiteUrl:{SiteCollection} Path:{Site} (IsDocument:”True” OR contentclass:STS_Site OR contentclass:STS_Web) FileExtension:aspx

SPSiteUrl:{SiteCollection} is used to restrict items to the current site collection. Without this clause, other site collections that are under managed paths (e.g. http://server/sites/mySC) may be included in the result when searching from the root site collection (e.g. http://server).

Path:{Site} is used to restrict items to those under the current site’s URL.

The rest should be self-explanatory.

The grouping/indenting by sites is performed in my custom display templates. To achieve this I had to ensure that the items are returned in the correct order, i.e. ordered by site and by URL depth.

I configured my search result to be sorted by SPWebUrl, then UrlDepth, and finally Path.

Note that the managed property SPWebUrl does not exist OOTB (at the time of writing). I had to create this in Central Admin and map it to the ows_taxId_SPLocationSite crawled property. I had to enable Query, Retrieve and Sort on this managed property.

The other thing is Sort is also not enabled by default for the Path managed property. I also had to change this in Central Admin.

If you are interested in the display templates then let me know and I’ll update the post.





About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Search, SharePoint. Bookmark the permalink.

One Response to Search query to find pages in current site and all sub-sites [SharePoint]

  1. Ramanjjilu Naidu says:

    would you please elaborate as i want to restrict the search results in one site collection.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s