Checking if a SPWeb Exists
Ran into a problem today where we were incorrectly trying to check if a web existed. We were trying:
private bool ChildWebExists(SPWeb parentWeb, string childWebName) {
return null != parentWeb.Webs[childWebName];
}
however this always returns an actual SPWeb object, turns out the correct way is to check the Exists property of the returned object ie:
private bool ChildWebExists(SPWeb parentWeb, string childWebName) {
using (SPweb childWeb = parentWeb.Webs[childWebName]) {
return childWeb.Exists;
}
}

Leave a Reply
You must be logged in to post a comment.