Tuesday, January 8, 2008

Working with sub folders in list

Hi all,

As you all know, in 2007 SharePoint introduced a new feature to lists - creating items in sub folders. But apparently the API was not modified in an easy way to support working with this feature...

In my last project I had to create sub-folders in lists and list items in these folders from code, and found that it is not so simple.

We are doing a rating solution for SharePoint items (coming soon on our web site, for more information contact sales@kwizcom.com), and wanted to create a new "item rating" list to store all ratings for all items on the same site.
For performence issues we wanted to create a sub folder for each list in the site and a second level folder for each item and create all ratings and comments in that folder.

The outcome of that project was 3 utilities methods that manages all that I need for working with folders and I thought it would be nice to publish them here - mainly because when I googled for it I didn’t find any good posts for that.

Here is how to use the utilities methods in your project:

SPList list = GetOrCreateList(web);//create your own...
//Create subfolder named "first level"
SPListItem folder = GetOrCreateFolderInList(list, list.RootFolder, "first level");
//Create subfolder named "second level"
folder = GetOrCreateFolderInList(list, folder.Folder, "second level");
//Create item in "second level" folder
SPListItem item = AddListItemInFolder(folder);
//Update meta data of new item
item["Rating"] = 5;
item["Comments"] = "";
item["ListID"] = list.ID.ToString("N");
item["ItemID"] = "ITEM_ID";
item["UserName"] = "shai...";
item.Update();


Here is the code for the utilities:

private SPListItem AddListItemInFolder(SPListItem parentFolder)
{
return parentFolder.ListItems.Add(parentFolder.Folder.ServerRelativeUrl, SPFileSystemObjectType.File);
}
private SPListItem GetOrCreateFolderInList(SPList parentList, SPFolder parentFolder, string folderName)
{
folderName = folderName.ToLower();
string parentFolderUrl = parentFolder == null ? "":parentFolder.ServerRelativeUrl.ToLower();
//Look in existing folders
foreach (SPListItem f in parentList.Folders)
if (f.Folder.ServerRelativeUrl.ToLower() == parentFolderUrl + "/" + folderName)
return f;//Found! return it!

//not exists - create
SPListItem folder = parentList.Items.Add(parentFolderUrl, SPFileSystemObjectType.Folder, folderName);
folder.Update();
return folder;
}


Well, hope this helps you - it sure did help me :)
Shai Petel,

10 comments:

Anonymous said...

This looks like it is just the thing I need. Thank you.

Anonymous said...

You have not included the code GetOrCreateList(). Any chance you could add that?

Shai Petel said...

GetOrCreateList is rather generic method to get a SPList...

Like:

SPList GetOrCreateList(SPWeb w)
{
SPList tmp = null;
try
{
//List name can be paramenter, what ever you want...
tmp = w.Lists["ListNameAsConst"];
}
catch//list does not exist - create it
{
w.Lists.Add(....);
tmp = w.Lists["ListNameAsConst"];
}
return tmp;
}


Just wrote it here - no Visual studio so test and fixt, but this is the general idea...

Anonymous said...

Thanks for the post! I couldn't find anything remotely as helpful anywhere else!

Anonymous said...

Thanks Shai. Your post helped me when I was almost at dead end. Thanks a lot again! :)

Anonymous said...

Hi! You saved my day, I have lost 3 hours to find a way to create Folder in List, but it was so simply! Get right Add() method and it works. Thank you!

Maulee Vadi said...

If I want to delete a .xslt file from a folder under Style library in MOSS 2007, how do I do that?

Maulee Vadi said...

How to I delete a file from a subfolder under Style Library in sharepoint?

Shai Petel said...

Hi Maulee,

there are several ways to get the SPFile object.
Once you have it you can use the file.Delete(); or file.Recycle(); methods.

To get the SPFile you can use its URL and get it from web.GetFile or go to your SPList.RootFolder.Folders["foldername"].Files collection.

(please excuse syntax errors i am not near visual studio)

Akalpita said...

hey your code is fabulous.saved me lot of time and effort in optimization of my code.