2

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?

1

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")
  • that´s wrong, you can use the server-object model for any code, running directly on the Server. You just have to Keep in mind, that there is not HttpContext, therefore you cant use SPContext.Current.... or running with elevatedprivileges – Leopold Lerch Jun 25 '15 at 9:32
  • @AsadRefai I get a System.IO.FileNotFoundException when running your suggestion stating: The Web application at http://mysite/ could not be found... I'm starting to wonder if its even possible to communicate with SharePoint from a stand alone console application. – KidBatman Jun 25 '15 at 16:23
  • @user2596756 Did you manage to solve the problem? – Asad Refai Jul 2 '15 at 11:31
  • 1
    @AsadRefai Yes. Since I am developing a console application I had to use objects found in the Microsoft.SharePoint.Client library as opposed to the Microsoft.SharePoint library which is used for applications deployed to the SharePoint server. – KidBatman Jul 2 '15 at 17:34
  • @user2596756 so your problem was due to the use of server object model. As when we try to access SharePoint from remote applications we need to use client object model. – Asad Refai Jul 2 '15 at 17:44
1

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

  • My console application and test project are both building under x64. Unit tests are also being run in x64 mode as well. Projects are both targeting .NET framework 4.0 as it needs to run against a SP2013 instance. – KidBatman Jun 25 '15 at 15:42

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.