SharePoint folder.exist

Isaac
Isaac used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You are accessing myweb which is out of scope ny the time you are accessing it.

using (SPWeb myWeb = mySite.OpenWeb())
            {                
               bool folderExist = CheckFolderExists(myWeb, caseFolder);


             }
IsaacSharePoint Client Side Developer

Author

Commented:
My apologies.  I copied and pasted wrong.  It's actually

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

Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

IsaacSharePoint Client Side Developer

Author

Commented:
I read that and a few other blog posts before coming to EE and they did not really help.

THanks
so if you return false instead of return folder.Exists you may have more success.
What exactly do you pass to CheckFolderExists as fldr param? It should be server relative url.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial