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("http://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("http://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 (10)
  1. Here is an assortment of various 2007 Microsoft Office SharePoint Server Documentation / Reference Materials…

  2. Jaime says:

    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();

  3. Andrew says:

    Do you know if the above code should work with the api for SharePoint 2007? (i’m using the Beta version 12.0.4017.1006). When the item.Update() is called i get an ‘Invalid URL exception’. I’ve made sure i’ve got the leading ‘/’ character of the url. Any ideas – could this be a bug in the beta version or am i doing something wrong?

  4. Harsh says:

    Andrew: yeah I created this code on 4017.1006..and am testing this on Beta2 TR as well. can you just post in your code here…so that I can look at whats causing the invalid URl exception?

  5. Harsh says:

    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…

  6. Dave Gardner says:

    Newsgroups and Discussions &amp;middot; SharePoint Portal Server Development &amp;middot; SharePoint Portal Server

  7. Tomas Foldyna says:

    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".

  8. Harsh says:

    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 🙂

  9. Thomas P says:

    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();

  10. 2007 MOSS Resource Links (Microsoft Office SharePoint Server) Here is an assortment of various 2007 Microsoft

Comments are closed.

Skip to main content