Get Site Collection From Web Application

In this blog, I will explain how to get all the Site Collections under a SharePoint web application with the help of SharePoint Search.

We can get the subsites under the site using CSOM easily but if we need to get all the site collections under a specific web application from a SharePoint farm, then we can’t get it directly. For that, we have to use SharePoint search. By using SharePoint Search, we set the “KeywordQuery” to filter and get the site collections. Before using the code block, we should ensure that the SharePoint Search Service Application is running and active.

The code block for this is mentioned below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Microsoft.SharePoint.Client;  
  8. using Microsoft.SharePoint.Client.Search.Query;  
  9. namespace GetSitecollFromWebApplication {  
  10.     class GetSitecollByUsingSearch {  
  11.         static void Main(string[] args) {  
  12.             string webAppUrl = string.Empty;  
  13.             string username = string.Empty;  
  14.             string password = string.Empty;  
  15.             NetworkCredential credentials = null;  
  16.             webAppUrl = "webAppUrl"//Please provide the Web Application URL  
  17.             username = "userName";  
  18.             password = "******";  
  19.             ClientContext ctx = new ClientContext(webAppUrl); //creating ClientContext from given SharePoint Site  
  20.             credentials = new NetworkCredential(username, password); //creating creadential object for SharePoint site.  
  21.             ctx.Credentials = credentials;  
  22.             // Before executing the below code please ensure Search Service Application is running  
  23.             KeywordQuery keyQuery = new KeywordQuery(ctx);  
  24.             keyQuery.QueryText = "contentclass:\"STS_Site\"";  
  25.             keyQuery.RowLimit = 500; // the maximum row limit is 500 for KeywordQuery  
  26.             keyQuery.EnableStemming = true;  
  27.             keyQuery.TrimDuplicates = false;  
  28.             SearchExecutor searchExe = new SearchExecutor(ctx);  
  29.             ClientResult < ResultTableCollection > resultTabColl = searchExe.ExecuteQuery(keyQuery);  
  30.             ctx.ExecuteQuery();  
  31.             var siteCollection = resultTabColl.Value.SelectMany(rs => rs.ResultRows.Select(r => r["Path"])).ToList();  
  32.             if (siteCollection != null && siteCollection.Count > 0) {  
  33.                 foreach(var site in siteCollection) {  
  34.                     Console.WriteLine(site);  
  35.                 }  
  36.                 Console.ReadLine();  
  37.             } else {  
  38.                 Console.WriteLine("Sorry, there were no Site Collections for the Web Application.");  
  39.                 Console.ReadLine();  
  40.             }  
  41.         }  
  42.     }  
  43. }  

In this blog, I have explained how you can get all the site Collections under a SharePoint web application. I hope this blog will help you out in a few scenarios.