SPSite.Exists(url) return false although the url exists
Asked Answered
L

3

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 !!

Linlithgow answered 9/12, 2011 at 7:8 Comment(0)
K
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.

Kinkajou answered 9/12, 2011 at 9:14 Comment(6)
Could you point me some websites where I can learn SharePoint from the basics or any books. Thanks in advance.Linlithgow
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...Kinkajou
just a note that site.OpenWeb(url, true) will fail if you have passed in an absolute URLLibra
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.Nan
@CameronVerhelst Thanks for the hint. Even though one is passing true to requiredExactUrl?Kinkajou
@Stefan, Yup, even with that parameter. Really weird, I wouldn't have expected it to do that, might be a bug in the method ?Nan
S
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;
    }
}
Smuts answered 15/1, 2015 at 11:17 Comment(1)
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.Pinchpenny
P
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;
}
Pardue answered 9/12, 2011 at 7:38 Comment(2)
What does path comparison do ?Linlithgow
-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.Kinkajou

© 2022 - 2024 — McMap. All rights reserved.