We help IT Professionals succeed at work.
Get Started

SharePoint folder.exist

463 Views
Last Modified: 2012-05-11
Something weird is happening when I try to see if a folder exist in a document library.
When CheckFolderExist() is called, it's always false no matter what ("NO FOLDER"), but when I get to the if statement (if (folderExist){}), it comes out true all the time no matter what.
Can somebody please explain? Does my code need to be modified.  It's been driving me crazy for the past 2 days.

using (SPSite mySite = new SPSite("http://landis/"))
        {
            using (SPWeb myWeb = mySite.OpenWeb())
            {                

             }
         }

string caseFolder = txtCaseNumber.Text.ToString();
.
.
bool folderExist = CheckFolderExists(myWeb, caseFolder);

 if (folderExist)
                        {
                            msg.Text += "Folder Exist<br />";
                        }
                        else
                        {
                            msg.Text += "None<br />";
                        }


private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            SPFolder folder = wb.GetFolder(fldr);
            if (!folder.Exists)
            {
                msg.Text += "NO FOLDER<br />";
                return false;
            }
            else
            {
                msg.Text += "FOLDER EXISTS<br />";
                return folder.Exists;
            }
        }
        catch
        {
            return false;
        }
    }

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE