Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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;

      }
    }
share|improve this answer
    
Nope, SPWeb.GetFolder doesn't return null if the folder doesn't exist. You must test if the property SPFolder.Exists is true. –  Dribbel Sep 21 '11 at 19:31
    
@Dribbel: You are 100% correct. I reposted code with Exists. –  Monica Lewinski Sep 21 '11 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 Jun 18 '13 at 15:22

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"

share|improve this answer
2  
Like this solution because of the neat and clear code. TryCatch is unnecessary. –  Anatoly Mironov Sep 29 '11 at 11:48

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;

      }
    }
share|improve this answer

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;
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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