Get Site Collection from Web Application

  • Posted by: Softree Technology

In this blog, I will explain how to get all the site Collection under a SharePoint web application by 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 will set the “KeywordQuery” to filter and get the site collections. Before using the code block, we should ensure the SharePoint Search Service Application is running and active.

The code block for this is mentioned below.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Net;  
using System.Text;  
using System.Threading.Tasks;  
using Microsoft.SharePoint.Client;  
using Microsoft.SharePoint.Client.Search.Query;  
namespace GetSitecollFromWebApplication {  
    class GetSitecollByUsingSearch {  
        static void Main(string[] args) {  
            string webAppUrl = string.Empty;  
            string username = string.Empty;  
            string password = string.Empty;  
            NetworkCredential credentials = null;  
            webAppUrl = "webAppUrl"; //Please provide the Web Application URL  
            username = "userName";  
            password = "******";  
            ClientContext ctx = new ClientContext(webAppUrl); //creating ClientContext from given SharePoint Site  
            credentials = new NetworkCredential(username, password); //creating creadential object for SharePoint site.  
            ctx.Credentials = credentials;  
            // Before executing the below code please ensure Search Service Application is running  
            KeywordQuery keyQuery = new KeywordQuery(ctx);  
            keyQuery.QueryText = "contentclass:\"STS_Site\"";  
            keyQuery.RowLimit = 500; // the maximum row limit is 500 for KeywordQuery  
            keyQuery.EnableStemming = true;  
            keyQuery.TrimDuplicates = false;  
            SearchExecutor searchExe = new SearchExecutor(ctx);  
            ClientResult < ResultTableCollection > resultTabColl = searchExe.ExecuteQuery(keyQuery);  
            ctx.ExecuteQuery();  
            var siteCollection = resultTabColl.Value.SelectMany(rs => rs.ResultRows.Select(r => r["Path"])).ToList();  
            if (siteCollection != null && siteCollection.Count > 0) {  
                foreach(var site in siteCollection) {  
                    Console.WriteLine(site);  
                }  
                Console.ReadLine();  
            } else {  
                Console.WriteLine("Sorry, there were no Site Collections for the Web Application.");  
                Console.ReadLine();  
            }  
        }  
    }  
}  

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

This solution is brought to you by our SharePoint professionals.

Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Author: Softree Technology

Leave a Reply