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"

SPOSearchSites

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"

SPOSearchSiteCollections

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"

SPOSearchSitesAll

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

SPSitesAllRestricted

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.

Comments

# re: How to: Query sites and site collections using SharePoint Search

Monday, January 23, 2012 2:39 PM by EmilyBen

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

# re: How to: Query sites and site collections using SharePoint Search

Tuesday, February 7, 2012 12:03 AM by CoreyRoth

@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.

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

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

# re: How to: Query sites and site collections using SharePoint Search

Thursday, November 17, 2016 10:05 AM by samolpp

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

Leave a Comment

(required)
(required)
(optional)
(required)