SharePoint 2010: Check if a folder exists in a SharePoint List

SharePoint 2010: Check if a folder exists in a SharePoint List

Here is the code to check, if the folder already exists in a SharePoint List.


private void CreateFolderInList(string folderName, string listName, SPListCollection listCollection)
{
    try
        {
                //Creating folder in "Sites" Lists
                SPList list = listCollection[listName];
 
                //Check if the Folder is already available in the list
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><Eq><FieldRef Name='Title'/><Value Type='Text'>" + folderName + "</Value></Eq><Eq><FieldRef Name=’FSObjType’/><Value Type=’Lookup’>1</Value></Eq></And></Where>";
 
  query.ViewAttributes = "Scope=\"RecursiveAll\""
 
 
                //Retrieve the items based on Query
                SPListItemCollection items = list.GetItems(query);
 
  //Item count is "0" if the folder does not exist
                if (items.Count == 0)
                {
                    folderItem = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
                    folderItem["Title"] = folderName;
                    folderItem.Update();
                    //return folderItem.Url;
                }
    }
        catch (Exception ex)
        {
        }
}

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Arun Kumar Arora edited Original. Comment: Changed text

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Arun Kumar Arora edited Original. Comment: Changed text

Page 1 of 1 (1 items)