How can i check if a subweb exists among SPWeb.Webs ? If i try web.Webs["SubWeb"] it will rise an exception.

Is the following solution clean ?

   SPWeb webfound= null;
   foreach (SPWeb subweb in web.Webs)
   if (subweb.Title == "SubWebTitle")
       webfound= subweb;
   if (webfound == null) return;
   // do stuff

2 Answers 2


This has some issues:

  • Performance: If you have many webs, many objects will be created.
  • Disposing: I'm not sure if these SPWeb objects needs to be disposed or if SharePoint keeps track of them and dispose the subwebs when the original SPWeb is closed.
  • Title: It is quite possible for multiple sub webs to have the same Title and for a user to change the Title, so Title is not a reliable identifier. Preferably use ID or URL
  • Permissions: In what context is the code running? Depending on context, your code may misbehave if the subweb exists, but the current user does not have access to see it.
  • Subsubwebs: If you need to find grandchildren, use SPSite.AllWebs

All things considered, I suggest simply using try/catch and trying to open the subweb, but using a different identifier (ID or URL instead of Title) than the original, and if neccessary changing SPSecurity.CatchAccessDeniedException to be able to catch also that case needs to be handled.


If you are using this code then you don't need to Worry about Object disposals and Permission related Issues.

  1. "Using" block will take care of objects disposal
  2. "SPSecurity.RunWithElevetedPrivileges(del)" will grant site collection admin permission to run this chunk of code

SPWeb myWeb = null; bool ifWebExists = false;

string webUrl = "http://yourSiteCollectionURL";
    using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb())
        SPWebCollection subSites = oWebsite.Webs;
        foreach (SPWeb subSite in subSites)
            if(subSite.Title == "SubWebTitle")
                ifWebExists = true;
                myWeb = subSite;

// do stuffs based on web existence
  • Actually that code does leak the SPSite object! With this code only the SPWeb will be disposed, the SPSite remains open. Additionally, whether RunWithElevetedPrivileges is appropriate or not depends on the context - for example it might expose confidential things the current user should not see. Jun 12, 2017 at 15:00
  • First thing first, SPSecurity.RunWithElevatedPrivilege will not expose any confidential or any other information to the user. It executes only the block of code which is written within these premise. Hence, In this case, only the web Titles are displayed. see reference : msdn.microsoft.com/library/….. Secondaly, We are not creating any SPSite object here in this case. We are only using SPSite.openWeb()
    – DvG
    Jun 12, 2017 at 17:45
  • The title or existence of a web may absolutely be confidential to a regular user, we don't know. SharePoint is very consistent about hiding everything that users can't access, unless requirements says otherwise we should do the same. Jun 13, 2017 at 5:03
  • An spsite is indeed created, that's what the "new" keyword does. It does not matter if it's assigned to a variable or not. Jun 13, 2017 at 5:05
  • And further regarding the elevated securities: The returned SPWeb will remain authenticated with the app pool account even outside the elevated code scope. Code using it will be able to read other things from it than the Title. Jun 13, 2017 at 5:34

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.