2

I have the following code:

SPFolder oFolder = myList.RootFolder.SubFolders[folderName];
            if (oFolder == null)
            {
                Console.WriteLine("FOLDER NOT FOUND"); 
            }
            else
            {
                Console.WriteLine("FOLDER FOUND"); 
            }

Looks good on the surface but it only works if the folder exists, if the folder doesn't exist it throws an object reference not set to an instance error.

Is there a better way to check if a folder exists (without iterating through every folder in a collection?

4 Answers 4

5

Try this...

public bool FolderExists(string url, SPWeb web)
    {
      try
      {
        //Check folder exist in the SPWeb or not

        if (web.GetFolder(url).Exists)
        {
          return true;
        }
        return false;
      }
      catch (ArgumentException ex)
      {
        return false;
      }
      catch (Exception ex)
      {
        return false;

      }
    }
3
  • Nope, SPWeb.GetFolder doesn't return null if the folder doesn't exist. You must test if the property SPFolder.Exists is true.
    – Dribbel
    Commented Sep 21, 2011 at 19:31
  • @Dribbel: You are 100% correct. I reposted code with Exists. Commented Sep 21, 2011 at 19:39
  • 1
    I don't think all of the try{}catch{} stuff is necessary here. Look at @Dribbel's answer for what I'm talking about.
    – iOnline247
    Commented Jun 18, 2013 at 15:22
9

The SPFolderCollection (which is returned by RootFolder) will throw an exception when queried for a non-existing folder. You can catch that Exception (an ArgumentException) or test it with a function like this:

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

use for the parameter folderName something like: "ListName/FolderName/SubFolderName"

2
  • 3
    Like this solution because of the neat and clear code. TryCatch is unnecessary. Commented Sep 29, 2011 at 11:48
  • Also this throws Exception, at least in SP2016 when run inside EventReceiver, so try/catch is needed.
    – Jussi Palo
    Commented May 22, 2020 at 11:49
1

Or you can try this...

public bool FolderExists(string url, SPWeb web)
    {
      try
      {
        //Check folder exist in the SPWeb or not

        if (web.GetFolder(url).Exists)
        {
          return true;
        }
        return false;
      }
      catch (ArgumentException ex)
      {
        return false;
      }
      catch (Exception ex)
      {
        return false;

      }
    }
1

You can create an extension method like this to check for the folder existance, and if exists you get the SPFolder in the output parameter.

SPFolderCollection[string] throw an ArgumentException if not exists so if might be faster ways to do this like using a CAML query, but here is a suggestion.

public static class SPFolderCollectionExtensions
{
    public static bool TryGetFolder(this SPFolderCollection instance, string folderName, out SPFolder folder)
    {
        try
        {
            folder = instance[folderName];

            if (!folder.Exists)
            {
                folder = null;
                return false;
            }

            return true;
        }
        catch (ArgumentException)
        {
            folder = null;
            return false;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.