Ethan Deng's SharePoint Blog

About Me
SharePoint
Blog
List Pagination 1
List Pagination 2
Customize Home Page
Excel Services Issue
Error with mngfield Page
User Defined Function
Enable Incoming Email
Install SP2 On R2
SharePoint Content Migrat
SharePoint2010 Installati
Troubleshoot Search
Dash Sign And Search
Search Pattern Issue
Create Dropdown Menu
Email Enabled Custom List
Email Enabled Issue List
SharePoint Folder
Cost Effective Small Farm
SharePoint Throttling
SystemUpdate And Alerts
A Parallel Approach
Create SharePoint Folders Programmatically
 
Conclusion
 
A SharePoint folder is not necessary a SharePoint content folder. A folder could be created but it is not shown anywhere in the SharePoint site as a content. Using SharePoint Designer you could see clearly there is a difference between a SharePint folder and a SharePoint content folder.
 
1. Create folder using SPFolderCollection
 
SPWeb.Folders and SPList.RootFolder.SubFolders all returns a SPFolderCollection which allows "Add" folder, but this folder is a SharePoint folder but not a SharePoint content folder. This folder shows in SharePoint Designer but not anywhere from browsing the conetnt in IE.
 
The only exception is that if the SPList is a document library, the folder created this way is also a SharePoint conent folder.
 

// Create a web site folder using SPFolderCollection but the folder is NOT content folder

web.Folders.Add("WebFolder1");

 

// Create a document library folder using SPFolderCollection, the folder is a SharePoint conent folder

SPList docList = web.Lists["MyDoc"];

SPFolder newFolder = docList.RootFolder.SubFolders.Add("DocFolder2");

 

// Create a list folder using SPFolderCollection, the folder is NOT a SharePoint conent folder

SPList myList = web.Lists["MyList"];

SPFolder newFolder2 = myList.RootFolder.SubFolders.Add("ListFolder2");

2. Create folder using SPListItemCollection

 

SPList.Items.Add should be used for both List and Document Library to create a SharePoint content folder. This is to ensure that your code work for both List and Document Library.

 

// Create a list folder using SPListItemCollection, the folder is a SharePoint conent folder

// The url is server relative url starts with / but not no trailing /

SPListItem newFolder3 = myList.Items.Add("/Lists/MyList", SPFileSystemObjectType.Folder, "ListFolder3");

// Must call update of the SPListItem

newFolder3.Update();

 

If you run this code and see the changes in SharePoint Designer

 
using (SPSite site = new SPSite(siteUrl))

{

Console.WriteLine("Site Url is site " + site.Url);

using (SPWeb web = site.OpenWeb())

{

// Test folder existing with SPFolder Exists

SPFolder folder1 = web.GetFolder("abc");

Console.WriteLine("folder abc: " + folder1.Exists);

 

// Create a web site folder using SPFolderCollection but the folder is NOT content

web.Folders.Add("WebFolder1");

 

// Create a document library folder using SPFolderCollection, the folder is a SharePoint conent

SPList docList = web.Lists["MyDoc"];

SPFolder newFolder = docList.RootFolder.SubFolders.Add("DocFolder2");

 

// Create a list folder using SPFolderCollection, the folder is NOT a SharePoint conent

SPList myList = web.Lists["MyList"];

SPFolder newFolder2 = myList.RootFolder.SubFolders.Add("ListFolder2");

 

// Create a list folder using SPListItemCollection, the folder is a SharePoint conent

// The url is server relative url starts with / but not no trailing /

SPListItem newFolder3 = myList.Items.Add("/Lists/MyList", SPFileSystemObjectType.Folder, "ListFolder3");

// Must call update of the SPListItem

newFolder3.Update();

}

}

 

 
You can see that some folders created using SPFolderCollection are not SharePoint content folder.
 
Other few notices about SharePoint folder are
 
1. SPWeb.GetFolder return SPFolder object event if the folder doesn't exist. Use SPFolder Exists to test the folder before using it.
 

// Test folder existing with SPFolder Exists

SPFolder folder1 = web.GetFolder("abc");

Console.WriteLine("folder abc: " + folder1.Exists);

2. docList.RootFolder.SubFolders.Add doesn't throw exception when folder exists.
 
 
3. Next code will throw exception if folder already exists.
 

SPListItem newFolder3 = myList.Items.Add("/Lists/MyList", SPFileSystemObjectType.Folder, "ListFolder3");

newFolder3.Update();

4. SPList.GetItems(query) will return a SPListItemCollection which include "Folder" item but not items in sub folders (non-recursive).
 
                SPQuery query = new SPQuery();
                query.Folder = currFolder;
                itemList = currList.GetItems(query);
5. SPList.Items will return all items recursively including all items from sub folders but it doesn't include any "Folder" item.