views:

934

answers:

1

Hi,

This is my code which checks if a SharePoint site exists or not.

string URL = Console.ReadLine();
using (SPSite objSite = new SPSite(URL))
{
    using (SPWeb objWeb = objSite.OpenWeb())
    {
      Console.WriteLine(string.Format("Site Exists: {0}", objWeb.Exists.ToString()));
    }
}

However, it doesn't seem to work. The "Exists" property always returns true even if the site/subsite does not exists. I get the same result when the URL is either "http://intranet" or "http://intranet/sitedoesnotexists".

Am I using this in the correct way?

Thanks

Edit

Sorry about the formatting, I was sure that I applied it but I guess I forgot.

+1  A: 

Try SPWeb.Exists property - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.exists.aspx

EDIT: Also, pople suggest using this overload of OpenWeb(..) method which has a "boolean" parameter: http://msdn.microsoft.com/en-us/library/aa543519.aspx to ask for exception if there is no such web site. (see it explained here: http://blog.mastykarz.nl/inconvenient-opening-spsite-openweb/)

naivists
I couldn't agree more... in fact I am one of those people who recommend using OpenWeb(..) http://kitmenke.com/blog/2009/06/08/the-danger-in-using-spsiteopenweb/
Kit Menke
Thanks, OpenWeb(...) with parameters worked great.
iHeartDucks