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

Microsoft SharePoint.NET ProgrammingC#

Avatar of undefined
Last Comment
Member_6283346
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
Isaac
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Isaac
Isaac
Flag of United States of America image

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
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo