Monday, December 14, 2009

Copy a folder from one list to another list programmatically.

using (SPSite site = new SPSite("http://testserver/spsite"))
{
using (SPWeb web = site.OpenWeb())
{
string sourceListUrl = "CustomList1/";
string destinationListUrl = "CustomList2/";

SPFolder rootFolder = web.GetFolder(sourceListUrl);
foreach (SPFolder objRootSubFolder in rootFolder.SubFolders)
{
if (objRootSubFolder.Name != "Forms")
{
folder = web.GetFolder(objRootSubFolder.ServerRelativeUrl);
foreach (SPFolder objFolder in folder.SubFolders)
{
objFolder.CopyTo(destinationListUrl + objRootSubFolder.Name + "/" + objFolder.Name);
}

foreach (SPFile objFile in folder.Files)
{
objFile.CopyTo(destinationListUrl + objRootSubFolder.Name + "/" + objFile.Name);
}
}
}
}
}

No comments:

Post a Comment