Вопрос

I am trying to determine if a site collection exists, but the SPSite.Exists method always seems to return false regardless if the site collection exists or not.

Snipp:

bool exists;
Uri uri = new Uri(mySite);

exists = SPSite.Exists(uri); //exists is always false
return exists;

In this case mySite == https...mysite.com/mySupposedSiteCollection. I can browse to the URL described, so why then does the method always return false? Am I trying to check the site collection incorrectly?


UPDATE

Please note that this is being called from a C# console application and is not a solution that is being deployed to the farm. Possibly a limitation i'm running into?

Это было полезно?

Решение

If you are trying to access site collection from console application which runs on Remote Machine, then I doubt you can use SPSite.Exists(uri) method because its a server object model.

However to check for site collection using c# with client object model, I have used below code.

We can use try catch block to determine if site collection exists, if the SC do not exists we can flag a variable in catch block.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isSiteCollectionExists = false;

            using (ClientContext context = new ClientContext("http://somesite.SharePoint.com"))
            {
                try
                {
                    var site = context.Site;
                    context.Load(site);
                    context.ExecuteQuery();
                    isSiteCollectionExists = true;
                }
                catch (Exception ex)
                {
                    isSiteCollectionExists = false;
                }
            }

        }
    }
}

In case you decide to go for server object model code, then below snippet can be helpful which is referred from How to Check if SharePoint Site Collection or Sub Site Exists

//To Check if site collection - "Sales" exists at the Web Application's Explicit inclusion Managed Path - "Sales" - <a href="http://somesite.sharepoint.local/Sales">http://somesite.sharepoint.local/Sales</a>
using (SPSite spRootSite = new SPSite("<a href='http://somesite.sharepoint.local/'>http://somesite.sharepoint.local</a>"))
{
    //Get the reference of the web application of the root site collection
    SPWebApplication spWebApp = spRootSite.WebApplication;
    if (!isSiteCollectionExists(spWebApp, "Sales"))
    {
        //Create New Site Collection
    }
}   

//Check if site collection exists at given web application
private static bool isSiteCollectionExists(SPWebApplication spWebApp, string siteCollectionRelativeUrl)
{
    bool returnVal = false;
    string webAppURL = string.Empty;
    foreach(SPAlternateUrl spWebAppAlternateURL in spWebApp.AlternateUrls)
    {
        if (spWebAppAlternateURL.UrlZone == SPUrlZone.Default)
        {
            webAppURL = spWebAppAlternateURL.Uri.AbsoluteUri;
        }
    }

    if (webAppURL.ToString().Length != 0)
    {
        Uri siteCollectionUri = new Uri(webAppURL + siteCollectionRelativeUrl);
        returnVal = SPSite.Exists(siteCollectionUri);
    }           
    return returnVal;
}


//More Samples
//To Check if site collection - "Sales" exists at the Web Application's Wildcard Inclusion Managed Path - "Sites" - <a href="http://somesite.sharepoint.local/Sites/Sales">http://somesite.sharepoint.local/Sites/Sales</a>
isSiteCollectionExists (spWebApp, "Sites/Sales")
//To Check if site collection - "Sales" exists at the Web Application's Wildcard Inclusion Managed Path - "Depts" - <a href="http://somesite.sharepoint.local/Depts/Sales">http://somesite.sharepoint.local/Depts/Sales</a>
isSiteCollectionExists (spWebApp, "Depts/Sales")

Другие советы

SP2013 is a x64 only application, therefore also their dlls work only in applications built for x64. Verify in you Project properties that it ist built for platform target x64 (not "any cpu") and try again

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top