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
THANKS Eugene !!! You saved my day (and my laptop too 'cause I was going to throw it somewhere …)
Thanks, I searched the whole internet for this and finally found it on your blog 🙂
I was using the first method, i tried some others, and i was never able to see the folder in the list.
THANK YOU, this post was usefull.
You know, this is one of those doggone microsoft things that just p's me off. Google won't find any results from 'workflow won't create the folder in debug mode'.
Thanks for posting this, you rock!
I am not sure I am following, but I would like to get my Summary Tasks (folders) to appear in a calendar view. Any suggestions?
Maybe this will help:
http://social.msdn.microsoft.com/Forums/hu/sharepoint2010general/thread/e7b11a43-009f-4f3c-b2b2-37ece7d2ecab
Thank you for posting! Can you advise on WHY folders created for Lists (either created in Win Explorer or in SharePoint Designer, for that matter) do not appear in the list view in the browser? I am unable to find anything that explains the reasoning.
I don't know for sure, but if I had to guess, I would say that lists use hidden folders to store things like views and content type data. Also, lists did not originally have the ability to store user-created folders through the object model whereas document libraries did. So I think that is why document library folders are automatically set up as list items in the library, but for lists you have to explicitly specify that a folder should also be a list item. Hope that helps.
Thanks- that does clarify.
Thank you this is very helpful. Good to see the "usings" in there too 🙂