Sharepoint - CSOM - Get all site collections for a Web Application

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.


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;
        }