6

Does anybody know why SPSite.Exists(url) return false although the url exists.

If I check the above statement, it returns false.

But I can open the site without any problems if I directly use

SPSite myRootSite = new SPSite(url);

Here is my coding .

if (SPSite.Exists(new Uri(url)))
{
     SPSite myRootSite = new SPSite(url);
}

UPDATE :

Sorry , I was calling SharePoint from one of my business layer which is not allowed.

My Fault !!

kevin
  • 13,559
  • 30
  • 79
  • 104

3 Answers3

15

The method SPSite.Exists checks whether the site-collection exists at the specified URL. But returns false if the URL points to a sub web of a site collection.

Given the following structure:

  http://server                  -> site collection
    http://server/web            -> sub web
    http://server/sites/somesite -> site collection
SPSite.Exists(new Uri("http://server")) // returns true
SPSite.Exists(new Uri("http://server/web")) // returns false
SPSite.Exists(new Uri("http://server/sites/somesite")) // returns true

If you want to check if there is any web at the given URL you have to use the method SPSite.OpenWeb(string url, bool requireExactUrl):

public static bool SiteExists(string url)
{
  try
  {
      using (SPSite site = new SPSite(url))
      {
        using (SPWeb web = site.OpenWeb(url, true))
        {
          return true;
        }
      }
  }
  catch (FileNotFoundException)
  {
    return false;
  }
}

The SPSite constructor takes any URL pointing to a sub element of the site-collection. Even if there is no element at the given location.

new SPSite("http://server/this/does/not/exist");

This snipped will return the site collection located at http://server.

While this is in most situations very useful there are situations where this is dangerous. Consider the following method:

public static void DeleteSite(string url)
{
  new SPSite(url).Delete();
}

If this method is called with http://server/this/does/not/exist the top most site collection at http://server will be deleted.

Stefan
  • 14,530
  • 4
  • 55
  • 62
  • Could you point me some websites where I can learn SharePoint from the basics or any books. Thanks in advance. – kevin Dec 09 '11 at 10:06
  • I don't know any site which are good for learning the SharePoint basics. I for my self learned this stuff by doing. What does not mean there aren't any book or sites out there... – Stefan Dec 09 '11 at 10:29
  • just a note that site.OpenWeb(url, true) will fail if you have passed in an absolute URL – Justin Stenning Mar 27 '13 at 03:47
  • 2
    I just found out that you're solution may give a false positive. In the inner most using for the SPWeb, instead of returning true you should return web.Exists, at least for SP 2013, where you will not get an error if the web doesn't exist. – Cameron Verhelst Sep 09 '14 at 07:01
  • @CameronVerhelst Thanks for the hint. Even though one is passing true to requiredExactUrl? – Stefan Sep 10 '14 at 08:29
  • 1
    @Stefan, Yup, even with that parameter. Really weird, I wouldn't have expected it to do that, might be a bug in the method ? – Cameron Verhelst Sep 11 '14 at 07:08
3

The answer from Stefan is almost correct. Here is the complete code, which should determine correctly, if a SPWeb exists on the given URL:

public static bool WebExists(string absoluteUrl)
{
    try
    {
        using (var site = new SPSite(absoluteUrl))
        {
            using (var web = site.OpenWeb())
            {
                return web.Exists;
            }
        }
    }
    catch (FileNotFoundException)
    {
        return false;
    }
}
Xaser
  • 31
  • 2
  • Be aware, the this method might return false positives. `OpenWeb` (when invoked without paramst) falls back to the nearest existing site in the path. For example if you have an URL `http://yoursite/existingweb/nonexistingweb`, it will open the web `http://yoursite/existingweb`, which does exist, and `web.Exists` will return true, even if the web with the original full URL does not exist. – pholpar Feb 14 '22 at 06:03
1

Actually the method SPSite.Exists tries to create a site from your Url and try-catches an exception. Besides that it performs paths comparison that might be unnecessary for you. So if you create your own method to check if the site exists that will be much more than OK.

this method might look like

public static bool SiteExists(string path){
 SPSite site;
 try{
  site = new SPSite(path);
 }
 catch(FileNotFoundException e)
 {
  return false;
 }
 finally
 {
  if(site!=null) site.Dispose();
 }
 return true;
}
Maks Matsveyeu
  • 866
  • 5
  • 11
  • What does path comparison do ? – kevin Dec 09 '11 at 08:02
  • -1: This method is will return `true` if there any other exception than a `FileNotFoundException` occurs. Plus, will return `true` for a URL pointing to a non-existant "sub site-collection". See my answer. – Stefan Dec 09 '11 at 09:16