check if SharePoint folder exists

How can I check if a SharePoint folder exist in a document library?

I'm using MOSS 2007 and programming with C#
Thanks.
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
abhitrigCommented:
Replace the line  if (!fldr.EqualsWith(""))

with
if(!string.IsNullOrEmpty(fldr))
0
 
jessc7Commented:
Make note that it apparently throws an exception rather than returning false.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I get the following error:

Object reference not set to an instance of an object. : c:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES\WebUserControl.ascx(95): error CS0117: 'string' does not contain a definition for 'EqualsWith'

What am I supposed to put within the double quotes of,   if (!fldr.EqualsWith(""))
foreach (GridViewRow row in GridView1.Rows)
            {                         
                HiddenField hdID = (HiddenField)row.FindControl("hdID");
                CheckBox complete = (CheckBox)row.FindControl("complete");
                DropDownList ddlStatus = (DropDownList)row.FindControl("status");
                TextBox txtCaseNumber = (TextBox)row.FindControl("caseNumber");
                DropDownList ddlPriority = (DropDownList)row.FindControl("priority");

  ----->  bool folderExist = CheckFolderExists(myWeb, txtCaseNumber.ToString());

                if (folderExist)
                {
                    msg.Text = "Folder Exist";
                }
                else
                {
                    msg.Text = "None";                        
                }                       

                if (complete.Checked)
                {
                        SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value))); 
                        byte[] fileBytes = item.File.OpenBinary();
                        string destUrl = destList.RootFolder.Url + "/" + item.File.Name;
                        SPFile destFile = destList.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);

                        msg.Text += "item.file.name is " + item.File.Name;
                    
                        // add the metadata to File            
                        SPListItem destItem = destFile.Item;
                        destItem["STATUS"] = ddlStatus.SelectedValue.ToString();
                        destItem["CASE NUMBER"] = txtCaseNumber.Text.ToString();
                        destItem["PRIORITY"] = ddlPriority.SelectedValue.ToString();
                        destItem["COMPLETE"] = complete.Checked.ToString();

                        destItem.Update();
                    
                        item.Delete();   
                    
                        //Re-bind the Grid after delete
                        GridView1.DataBind();                                       
                }
           }
    }

//************************
    private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            if (!fldr.EqualsWith(""))
            {
                SPFolder folder = wb.GetFolder(fldr);
                return fldr.Exists;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.