Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

check if SharePoint folder exists

Posted on 2011-05-09
4
Medium Priority
?
2,061 Views
Last Modified: 2012-06-21
How can I check if a SharePoint folder exist in a document library?

I'm using MOSS 2007 and programming with C#
Thanks.
0
Comment
Question by:Isaac
  • 2
4 Comments
 
LVL 16

Expert Comment

by:jessc7
ID: 35725390
0
 
LVL 16

Expert Comment

by:jessc7
ID: 35725400
Make note that it apparently throws an exception rather than returning false.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35730507
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
 
LVL 14

Accepted Solution

by:
abhitrig earned 2000 total points
ID: 35731479
Replace the line  if (!fldr.EqualsWith(""))

with
if(!string.IsNullOrEmpty(fldr))
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question