Dot Net Mafia

Group site for developer blogs dealing with (usually) Ionic, .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog



Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for Ionic, SharePoint, and Office 365.

How to: Query sites and site collections using SharePoint Search

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.



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.



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:


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.



EmilyBen said:

Any way you do this exact thing but display only Blogs.

January 23, 2012 2:39 PM

CoreyRoth said:

@EmilyBen Maybe.  However, blog posts aren't one of the standard STS content classes you see in a traditional list.  I bet it has a class though.  You would need to look it up though.  Add the ContentClass managed property to your search results page and then issue a query that has blog posts in it.  You can get the value and then reuse it in the query like above.

February 7, 2012 12:03 AM

Search Center | List of site and site collections | SharePoint 2010 | Trailblazer's Sharepoint Blog said:

Pingback from  Search Center | List of site and site collections | SharePoint 2010 | Trailblazer's Sharepoint Blog

March 16, 2016 7:44 PM

samolpp said:

can i get the all sub sites for the current logged in user ?using this serach api ? pls help

November 17, 2016 10:05 AM

Leave a Comment


About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2018 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems