in

Dot Net Mafia

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

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

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.

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

 

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

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an Applications Architect at Infusion specializing in ECM and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems