Point Deep

Mundeep's Tech Blog

Checking if a SPFolder Exists

Posted by mundeep on February 24, 2009

Ran into a colleague’s code that was incorrectly trying to check if a folder existed. It was something like:

private bool CheckFolderExists(SPWeb parentWeb, string folderName) {
    SPFolder folder = parentWeb.GetFolder(folderName);
    if (folder == null) {
        return false;
    }
    else {
         return true;
    }
}

however this always returns an actual SPFolder object, and the correct way is to check the Exists property of the returned object ie:

private bool CheckFolderExists(SPWeb parentWeb, string folderName) {
    SPFolder folder = parentWeb.GetFolder(folderName);
    return folder.Exists;
}

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

Advertisements

10 Responses to “Checking if a SPFolder Exists”

  1. Ulrich said

    If it worked as easy as this we would all be very happy puppies. In reality, SPFolder.Exists does return true if the folder exists but throws an exception if it doesn’t. Poor coding practice? Depends on how you look at it: As a non-admin, you are probably not supposed to successfully guess at the existence or non-existence of objects. Some MS developer might have thought about this and came up with this solution. Which is not helpful, of course.

  2. Zick said

    Hello,

    thanks for the tip, it will be usefull for me 😉

  3. gordy said

    try with:

    private bool CheckFolderExists(SPWeb wb,string fldr)
    {
    try{
    if(!fldr.EqualsWith(“”){
    SPFolder folder=wb.GetFolder(fldr);
    return fldr.Exists;
    }else{
    return false;
    }

    }
    catch
    {
    return false;
    }
    }

  4. Shafaqat Ali said

    HI

    I am trying to check the availibility of parent folder but folder.exists always returns fasle,what could be the problem?

  5. Anonymous said

    hey thanks buddy,
    it saved my time……

    Coolguy.

  6. Make sure to wrap in a try catch and handle accordingly as per @Gordy.

    Thanks for the code.

  7. ludo said

    if (folder == null) {
    return false;
    }
    else {
    return true;
    }

    Please stop doing this ! Use :
    if (folder == null)
    return false;
    return true;

  8. mundeep said

    May i ask why? Both if statements are logically the same i find if () {} else {} to be more readable.
    http://stackoverflow.com/questions/3316305/what-is-difference-if-i-dont-use-the-else-condition

  9. In SharePoint 2010 the SPFolder.Exists property is implemented like this so it should not throw any exception (havn’t checked SP2007 though):

    public bool Exists
    {
    get
    {
    bool result = false;
    try
    {
    Hashtable hashtable = this.PropertiesCore(false);
    result = (hashtable != null);
    }
    catch
    {
    result = false;
    }
    return result;
    }
    }

  10. […] 1. Checking if a SPFolder 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: