Check to see if a SharePoint site exists or not

17,212

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/)

Share:
17,212
iJK
Author by

iJK

Updated on June 05, 2022

Comments

  • iJK
    iJK almost 2 years

    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.