How to: Query sites and site collections using SharePoint Search
Posted
Friday, January 13, 2012 1:41 PM
by
CoreyRoth
I often refer to my Handy Keywords in SharePoint Search because it forms the building blocks you need to become a rock star at writing SharePoint keyword queries. Today, I want to expand upon that post on how you can use search to return a list of sites or site collections. This is useful when you want to quickly inspect a SharePoint farm using the Search Center. The secret behind returning sites with Search is use of the contentclass managed property. You just have to know what value to specify. To return a list of all sites in the search index, we specify a value of STS_Web (remember our translation guide). Here is what it looks like.
contentclass:"STS_Web"
This works pretty well. However, if you inspect the results on your own server, you might quickly notice one thing missing. The root web of all the site collections is not present in the search results. To get the site collections, we specify STS_Site instead.
contentclass:"STS_Site"
Notice, I now have all of the site collections on my particular tenant. If you want the results combined, you simply combine the queries.
contentclass:"STS_Site" contentclass:"STS_Web"
Now you might be thinking this is great, but you don’t want every site and site collection on the farm. You want to restrict it to a particular web application. That’s actually pretty easy, just refer back to the handy keywords post and use the Site managed property. In my case, I’ll restrict the URL to my main web application.
contentclass:"STS_Site" contentclass:"STS_Web" site:https://dotnetmafia.sharepoint.com
There are lots of ways to get lists of sites in SharePoint, but I find this one works well since it doesn’t require a line of code for you to try. Of course, as a developer, you can make use of this with the KeywordQuery class inside your own application. This works with SharePoint on-premises as well as SharePoint Online. All screenshots were from my personal SPO site.