17 March 2009

Creating folders within an SPList : MOSS 2007

SharePoint allows creation of folders within a list. This has a lot of advantages some of which are mentioned below.

  • Segregation of related list items.
  • Easy management of List operations like add, delete, update and search.
  • Easy management of permissions as related items can be assigned permissions at the folder level.
Usually, there is a requirement of creating these folders dynamically using code.

SPSite oSPSite = new SPSite("http://mysharepointsite");
SPWeb oSPWeb= oSPSite.OpenWeb("mywebsite");

SPList oSPList = oSPWeb.Lists["Country"];
SPListItem oSPListItem = oSPList.Folders.Add(oSPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);

oSPListItem["Name"] = "France";

The first parameter of Folders.Add method mentions the level at which the item has to be created. In the above example we specified that the folder should be created at the root level within the list.
The second parameter mentions the type of list item to be created.

No comments:

Post a Comment