Unlike adding folders to document libraries, adding folders to lists requires a bit more work in order for the folder to appear in the list view. For example, the following code will add a folder to the list, but the folder will be hidden and not show up in the list view:

   1: //get a reference to the SharePoint objects
   2: using(SPSite reportingSiteCollection = new SPSite(“http://localhost”))
   3: {
   4:     using(SPWeb reportingSite = reportingSiteCollection.OpenWeb())   
   5:     {
   6:         //get a reference to the list
   7:         SPList reportingList = reportingSite.Lists[“Reporting List”];
   8:         
   9: //the following line will in fact create the folder, but the folder within the list, but there will be no list item associated with the folder, so the folder will not appear in any list views
  10:         reportingList.RootFolder.SubFolders.Add(“New_Folder_Name”); 
  11:     }
  12: }

Instead, you need to add a list item using the SPFileSystemObjectType.Folder enumeration and call the update method on the newly created list item so that the folder the item represents will not only be added to the list but also be visible as a list item:

   1: //get a reference to the SharePoint objects
   2: using(SPSite reportingSiteCollection = new SPSite(“http://localhost”))
   3: {
   4:     using(SPWeb reportingSite = reportingSiteCollection.OpenWeb())
   5:     {
   6:         SPList reportingList = reportingSite.Lists[“Reporting List”];
   7:
   8:         //the following line will in fact create the folder, but the folder within the list, but there will be no list item associated with the folder, so the folder will not appear in any list views
   9:         SPListItem newFolder = reportingList.Items.Add(reportingList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, “New_Folder_Name”); 
  10:
  11:         //the following line will associate a list item with the newly created folder and make the folder visible in the list views
  12:         newFolder.Update();
  13:     }
  14: }

Technorati Tags: C#,WSS,SharePoint,MOSS