Adding a List Item inside a folder
As with v2, v3 also has seen its fair share of requests on how you can add an item to a folder in a list. Now, there are two types of lists that we are dealing with, lists that store documents and lists that store items. Internally they are identified by the list type which can be a DocumentLibrary or a GenericList. Actually the enumeration is quite huge, but these are the two values that we are concerned with. Both these types of lists can have folders and these folders can have items in them. Adding items to the folders though the UI is pretty straight forward. On the other hand, doing the same through code is a bit tricky. The SDK documents it to an extent but that is not very clear.
Adding a document to a folder in a document library is the simplest
SPSite site = new SPSite("https://blr3r01-13:10000");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Documents"];
SPListItemCollection folderColl = list.Folders;
foreach (SPFolder folder in folderColl)
{
if (folder.Name == "TestFolder")
{
SPFileCollection fileColl = folder.Files;
// Use fileColl.Add() to upload the file
}
}
Adding an item to a folder for a generic list took me some time to figure out. The point where I got mislead was believe it or not a missing '/' character. There were a number of things I tried in getting to the SPListItemCollection representing the items inside a folder. However, all attempts at getting there ended up in recurrent loops [sic]. The simplest approach was of course using the SPListItemCollection.Add() . The moment I finally obtained that collection, from there on it took me several tries to get the actual syntax right. So here's the polished up code.
SPSite site = new SPSite("https://blr3r01-13:10000");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Tasks"];
SPListItemCollection folderColl = list.Folders;
for (int i = 0; i < folderColl.Count; i++)
{
if (folderColl[i].Folder.Exists)
{
SPFolder folder = folderColl[i].Folder;
SPListItemCollection itemColl = list.Items;
SPListItem item = itemColl.Add("/Lists/Tasks/TestFolder", SPFileSystemObjectType.File, null);
item["Title"] = "AddedFromOM";
item.Update();
}
}
All in a day's work.
Cheerio
/Harsh
Blogged using Microsoft Office Word 2007 running on Microsoft Vista
Comments
- Anonymous
August 14, 2006
Here is an assortment of various 2007 Microsoft Office SharePoint Server Documentation / Reference Materials... - Anonymous
August 25, 2006
I tried to create new folders as you in your example, but I have a problem. First of all, the only thing I can create are files to the lists.
I saw that you have a collection in list element called Folders that I don't. I suppose we are using different versions of SharePoint, otherwise what?
This is my sintax, is there something wrong?
SPWeb web = SPControl.GetContextSite( this.Context ).OpenWeb();
SPList list = web.Lists[TextBoxCarpeta.Text];
SPListItem item = list.Items.Add(); - Anonymous
August 28, 2006
The comment has been removed - Anonymous
September 05, 2006
The comment has been removed - Anonymous
September 05, 2006
Jaime:
I am running this on MOSS Beta2/Beta2 TR...
Are you by any chance running this against v2/2003? My guess is that it should still work... - Anonymous
October 01, 2006
Newsgroups and Discussions &middot; SharePoint Portal Server Development &middot; SharePoint Portal Server - Anonymous
October 29, 2006
Hi, i used this code, and it works succesfully ... SPList list = .... (some list) // create new folder in a List SPListItem newFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder); if (newFolder != null) { newFolder.Name = "XXXX"; newFolder.Update(); } else { EventLog.WriteEntry("Error","Cannot add folder ..."); } ...
ServerRelativeUrl - it is most important, my code does't work if i used directly parent folder as example "/Lists/Tasks".
Anonymous
November 28, 2006
Thanks Tomas for mentioning that. Though I wonder why that would be. Checking the implementation of the Add method might reveal something is what I guess. Lets see what gives :)Anonymous
December 22, 2006
I've writen a post on this.. its easier.. use the overriden Add method with myFolder.ServerRelativeUrl. My code is an example of adding an item to a list in a spec. folder and giving it a spec. content-type. SPContentType myCType = myList.ContentTypes["Custom content type"]; SPList myList = myWeb.Lists["My First List"]; *SPFolder myFolder = dm.myList.Folders[3]; *SPListItem myNewItem = dm.myList.Items.Add(myFolder.ServerRelativeUrl, SPFileSystemObjectType.File, null); myNewItem["ContentTypeId"] = myCType.Id; myNewItem.Update(); myNewItem["Title"] = "New Item"; myNewItem["CustomSiteColumn"] = 3; myNewItem.Update();Anonymous
September 13, 2007
2007 MOSS Resource Links (Microsoft Office SharePoint Server) Here is an assortment of various 2007 Microsoft