4

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.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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