Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

SharePoint folder.exist

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

Avatar of SharePointGirl
SharePointGirl
Flag of United Kingdom of Great Britain and Northern Ireland image

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


             }
Avatar of Isaac

ASKER

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

SOLUTION
Avatar of SharePointGirl
SharePointGirl
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial