Point Deep

Mundeep's Tech Blog

Archive for December, 2007

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

Posted in Sharepoint | Tagged: , | 1 Comment »