Lists, Folders and List Items

One of the initiatives for WSS 3.0 is to more closely align lists and libraries making them easier for end users and developers to switch between the two. One of the new features of WSS 3.0 is the ability to create folders inside of lists. In WSS 2.0 we were limited to only folders in document libraries.

Folders are really a “metadata” concept. Data in WSS 3.0 is stored in one or more SQL Server databases and folders as we know exist only in a semantic sense in a database. Nonetheless using folders to provide a visual clue to organization can be beneficial to your users.

Creating a New Folder in the Task List

Creating a folder in the user interface is simple. Click the list’s New button and select folder. I should clarify that WSS 3.0 lists can use and contain folders but a list’s definition may not allow it as a valid object in the list instance. If you are working in a list and the folder type is not available under the New button then the list does not allow folders in it’s current status.

Image creating a new folder in a list.

 

Adding a New List Item in a Folder

Creating a list item within a folder using the GUI is simple as well. Simple open a folder and then select the New button. Pick the new item and it will ultimately exist in the folder.

Image creating task list item.

Creating a SPListItem in a SPFolder Using the OM

Creating a folder (SPFolder) using the WSS 3.0 object model is simple. Creating a list item (SPListItem) in a list is simple as well. It took me awhile and a number of emails to finally create a list item in an existing folder. So here is the code to save you some time.

SPSite site = new SPSite(“http://[ServerName/sitename]”);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[“Tasks”];

SPFolder f = web.GetFolder(“http://[ServerName/sitename]/Lists/Tasks/Test” );
if(f.Exists)
{
SPListItemCollection itemColl = list.Items;
SPListItem item = itemColl.Add(f.ServerRelativeUrl , SPFileSystemObjectType.File, null);
item[“Title”] = “Added from OM”;
item.Update();
}

In this example I have opened the web containing the Task list I want to update and get a reference to the Task list instance. I create a folder using SPWeb’s GetFolder method by passing in the folder’s url. I can now check to see if I have a reference to a valid folder by using the SPFolder’s Exists method. A check for null does not work as the call to the GetFolder method appears to always return a SPFolder object even if there is no corresponding folder at the location specified.

Now that I know that the folder exists in the list I can create a new list item. The key is to pass in the SPFolder’s ServerRelitiveUrl and SPFileSystemObjectType.File. Not sure why the SPFileSystemObjectTypetype is FileI would think that Folder would have worked nicely.

Once we have the SPListItem created in the folder we can add values to the fields and call the SPListItem’s Update method to save the changes.
The key to creating a SPListItem in an existing folder is to verify the folder exists and use the SPListItemCollection’s Add method passing in the folder’s server relative url and the SPFileSystemObjectType.File value .