6

How to get all site collections for a web application using CSOM(SharePoint on-premises). There are ways to do the same for SharePoint online. But I couldn't find a way to do it for SharePoint on-premises.

5 Answers 5

3

You need to use SSOM code, shown as following:

public static SPSite GetSite(SPFarm farm, string absoluteUrl, bool isThrowErrorIfDoesntExist)
        {
            SPSite site = null;
            try
            {
                // 初始化
                if (farm == null) { throw new Exception("SPFarm passed is null"); }
                if (!Validation.IsValidUrl(absoluteUrl)) { throw new Exception("url passed is invalid"); }
                absoluteUrl = absoluteUrl.ToLower().Trim();

                foreach (SPService service in farm.Services)
                {
                    if (service is SPWebService)
                    {
                        SPWebService webService = (SPWebService)service;
                        foreach (SPWebApplication webApp in webService.WebApplications)
                        {
                            foreach (SPSite _site in webApp.Sites)
                            {
                                if (_site.Url.ToLower().Trim() == absoluteUrl)
                                {
                                    site = _site;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (site == null && isThrowErrorIfDoesntExist) { throw new Exception("Failed to find site using url: " + absoluteUrl); }
            }
            catch (Exception ex)
            {
                Log.WriteError("uilryujgcxf5yfdh", ex, "url: " + absoluteUrl);
                site = null;
            }
            return site;
        }
3

I dont think there is any direct option to get list of all site collection in CSOM (SharePoint on-premises) as CSOM can access only site collection or below level objects. But you can always try to use some webservice and call it from your client side code.

0

I never did this in CSOM but using Microsoft.SharePoint.Administration you can access a SPWebApplication wich contains a property called Sites wich contains a collection of every site collection in this web application. As mentioned i never used it in CSOM before so i don't know if it's possible.

0
0

I don't think you can do that in CSOM. However you can do that with a SharePoint Core Solution project and write a feature which does that.

It is old technology, but it still works.

You can use the following code to loop through the site collection:

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;

foreach (SPSite siteCollection in siteCollections)
{
    Label1.Text += siteCollection.Url + "<BR>";

    siteCollection.Close();
}
0

There is a way to do this using CSOM but it does require access to your SharePoint_Config database (or whatever you have named it in your environment). If you have your farm credentials, you should also be able to find the GUIDs you need using PowerShell for the Web Application for which you are trying to access.

Get-SPWebApplication <webapplicationUrl> | Format-List * 

This should give you the GUID of the web application for which you are trying to fetch the site collections.

Next:

using System.Data.SqlClient;
using Microsoft.SharePoint.Client;
//and all your other "using"'s for your application

//first, get your connection string from your app.json file    
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["CONNECTIONSTR"].ToString();
//get webAppId from your powershell command and store it in webAppId setting in your app settings
string sitesQueryString = "SELECT [Path] FROM [Sharepoint_CONFIG].[dbo].[SiteMap] WHERE [ApplicationId] = '" + ConfigurationManager.AppSettings["webAppId"].ToString() + "'";
//also store webAppUrl in appsettings
string webapp = ConfigurationManager.AppSettings["webAppUrl"].ToString();

//retrieve sites into DataTable object
DataTable dtSites = new DataTable();
SqlConnection sqlConn = new SqlConnection(connString);
SqlDataAdapter sqlDASites = new SqlDataAdapter(sitesQueryString, sqlConn);
sqlDASites.Fill(dtSites);
//Iterate through sites (actually, their root webs).
foreach (DataRow site in dtSites.Rows)
{
    Console.WriteLine("Site: " + webapp + site["Path"].ToString());
        
    ClientContext clientContext = new ClientContext(webapp + "/" + 
    site["Path"].ToString());
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();
 
    //to get parent site collection, use (I might be wrong about this)
    Site site = clientContext.Current.Site;     
    clientContext.Load(site);
    clientContext.ExecuteQuery();
}

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.