Code Snippet – How to Check if SharePoint Site Collection or Sub Site Exists

Recently I have been focusing on building site provisioning tool for one of my clients and one of the most important code snippets was to check whether given site collection or sub site exists whenever end-user requests new site collection or sub site. Even though SharePoint 2010 (or even SharePoint 2007) has been released for a while, I was surprised to see that there isn’t any complete code snippets on web. There are many MS and Non-MS forum questions and responses but I couldn’t find exactly what I was looking for as far as code snippets.

Here is the code snippet to check if site collection exists in given web application. Notice that following code uses SPSite.Exists method to check if site collection exists at given path.

//Check if site collection exists at given web application
private static bool isSiteCollectionExists(SPWebApplication spWebApp, string siteCollectionRelativeUrl)
{
    bool returnVal = false;
    string webAppURL = string.Empty;
    foreach(SPAlternateUrl spWebAppAlternateURL in spWebApp.AlternateUrls)
    {
        if (spWebAppAlternateURL.UrlZone == SPUrlZone.Default)
        {
            webAppURL = spWebAppAlternateURL.Uri.AbsoluteUri;
        }
    }

    if (webAppURL.ToString().Length != 0)
    {
        Uri siteCollectionUri = new Uri(webAppURL + siteCollectionRelativeUrl);
        returnVal = SPSite.Exists(siteCollectionUri);
    }           
    return returnVal;
}

//To Check if site collection - "Sales" exists at the Web Application's Explicit inclusion Managed Path - "Sales" - <a href="http://niks.sharepoint.local/Sales">http://niks.sharepoint.local/Sales</a>
using (SPSite spRootSite = new SPSite("<a href="http://niks.sharepoint.local/">http://niks.sharepoint.local</a>"))
{
     //Get the reference of the web application of the root site collection
     SPWebApplication spWebApp = spRootSite.WebApplication;
     if (!isSiteCollectionExists(spWebApp, "Sales"))
     {
           //Create New Site Collection
     }
}

//More Samples
//To Check if site collection - "Sales" exists at the Web Application's Wildcard Inclusion Managed Path - "Sites" - <a href="http://niks.sharepoint.local/Sites/Sales">http://niks.sharepoint.local/Sites/Sales</a>
isSiteCollectionExists (spWebApp, "Sites/Sales")
//To Check if site collection - "Sales" exists at the Web Application's Wildcard Inclusion Managed Path - "Depts" - <a href="http://niks.sharepoint.local/Depts/Sales">http://niks.sharepoint.local/Depts/Sales</a>
isSiteCollectionExists (spWebApp, "Depts/Sales")

Here is the code snippet to check if sub site exists in given site collection. Notice that following code uses SPWeb object’s Exists property to check if sub site exists at given path. Additionally, it opens the web using OpenWeb(String, Boolean) overloaded method where second parameter “true” means exact URL is supplied.

//Check if sub site exists at given site collection
private static bool isWebExists(SPSite spSite, string webRelativeUrl)
{
    bool returnVal = false;
    using (SPWeb currentWeb = spSite.OpenWeb(webRelativeUrl, true))
    {
        returnVal = currentWeb.Exists;
    }
    return returnVal;
}
//To Check if sub site- "Chicago" exists at Site Collection - <a href="http://niks.sharepoint.local/HR">http://niks.sharepoint.local/HR</a>
using (SPSite spSite = new SPSite("<a href="http://niks.sharepoint.local/HR">http://niks.sharepoint.local/HR</a>"))
{
     if (!isWebExists(spSite, "Chicago"))
     {
         //Create New Web
     }
}

You can also use following code snippet to check if sub site exists in given site collection. Instead of using SPWeb.Exists property, this code trying to access one of the SPWeb object property to check whether object was instantiated. If there is no sub site, it would throw and exception and silently return the false.


private static bool isWebExists(SPSite spSite, string webRelativeUrl)
{
     bool returnVal = false;
     using (SPWeb currentWeb = spSite.OpenWeb(webRelativeUrl))
     {
          try
          {
              //Try to access the site property. If it doesn't throw an error, means site already exists.
              string siteTitle = currentWeb.Title;
              returnVal = true;
           }
           catch
           {
               //Do nothing
           }
     }
     return returnVal;
}

Advertisements
This entry was posted in SP2010 Code Snippets. Bookmark the permalink.

One Response to Code Snippet – How to Check if SharePoint Site Collection or Sub Site Exists

  1. Pingback: Code Snippet – How to Check if SharePoint Site Collection or Sub Site Exists | Office 365 and SharePoint with Nik Patel | SPOmani

Comments are closed.