Point Deep

Mundeep's Tech Blog

Checking if a SPWeb Exists

Posted by mundeep on December 19, 2007

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

One Response to “Checking if a SPWeb Exists”

  1. […] NB: Yes, this post is almost identical to my earlier one about checking if an SPWeb object exists. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: