1

How can i check if spsite exist or not ?

I have a web address www.portal2010.com/internalportal/employeesites

Now when i try to open web and spsite it opens upper level spsite instead

spsite mysite = new site("www.portal2010.com/internalportal/employeesites")

How can I check if web exists with address www.portal2010.com/internalportal/employeesites or not?

3

You have to check the SPWeb object of the subsite to check whether it exists,

using(SPSite mysite = new SPSite ("www.portal2010.com/internalportal/employeesites"))
    {
     using (SPWeb web = site.OpenWeb("/internalportal/employeesites"))
      {
        if(web.Exists)
        {
          //Web Exist
        }
      }
    }
  • problem is that, site collection address is www.portal2010.com and "/internalportal/employeesites" doesn't exists so it opens root web instead and donot throw any error as well, i am not knowing child web name as its dynamix – Mathematics Aug 30 '13 at 13:30
  • 1
    This actually works, have you tried to execute OpenWeb method with url parameter, not a plain OpenWeb()? It does not throw exception, but web.Exists returns false in case of non-existing site – Tim Aug 30 '13 at 13:45
  • it's opening wrong web (root web instead), how can i use web.exists ?? – Mathematics Aug 30 '13 at 14:09
1

Considering your sitecollection address is "www.portal2010.com/internalportal/employeesites". then below code won't give error as OpenWeb() opens web object associated with the site collection

using (SPSite site = new SPSite("www.portal2010.com/internalportal/employeesites"))
{
    using (SPWeb web = site.OpenWeb())
    {

    }                
} 

However, if say you want to go into a subsite that may or may not exist at www.portal2010.com/internalportal/employeesites/subsite, you can write following code to check if web exists or not

using (SPSite site = new SPSite("www.portal2010.com/internalportal/employeesites"))
{
    using (SPWeb web = site.OpenWeb("subsite"))
    {
        if (web.Exists)
        {
        }
    }                
}

Also, you can't open a site collection which doesn't exist. Following code will throw error: while trying to create SPSite object

using (SPSite site = new SPSite("www.portal2010.com/internalportal/wrongSiteCollection"))
{
    if (site.OpenWeb().Exists)
    {

    }                          
}
  • problem is that, site collection address is www.portal2010.com and "/internalportal/employeesites" doesn't exists so it opens root web instead and donot throw any error as well, i am not knowing child web name as its dynamix – Mathematics Aug 30 '13 at 13:27
0
private bool ChildWebExists(SPWeb parentWeb, string childWeb)
{     
   using (SPweb childWebexists = parentWeb.Webs[childWeb]) 
   {         
       return childWebexists.Exists;     
   } 
} 

it returns a bool value

string childWeb == Gets the web site object witht the specified site-relative url from the collection so it would be the string site name, you can change it to a guid or id instead of string!

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.webs.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.exists.aspx

EDIT

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;
 } 

if it doesnt exist it will throw an error and as bool is set to false will return false!

http://nikspatel.wordpress.com/2011/11/30/code-snippet-how-to-check-if-sharepoint-site-collection-or-sub-site-exists/

  • problem is that, site collection address is www.portal2010.com and "/internalportal/employeesites" doesn't exists so it opens root web instead and donot throw any error as well, i am not knowing child web name as its dynamix – Mathematics Aug 30 '13 at 13:27
  • your right, as its dynamic that would be an issue: check out my ammended answer! – Ali Jafer Aug 30 '13 at 13:34
  • 3
    You can just return currentWeb.Exists property instead of trying to access Title property. Generating unnecessary exceptions in code is not a good practice – Tim Aug 30 '13 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.