Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPSite.Exists(url) return false although the url exists

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

like image 597
kevin Avatar asked Dec 09 '11 07:12

kevin


3 Answers

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.

like image 139
Stefan Avatar answered Nov 17 '22 07:11

Stefan


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;
    }
}
like image 21
Xaser Avatar answered Nov 17 '22 06:11

Xaser


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;
}
like image 28
Maks Matsveyeu Avatar answered Nov 17 '22 07:11

Maks Matsveyeu