As I just blogged about finding items within a given folder, the same concept can then be enhanced to copy over an entire list with folders and items, to another list of the same structure.
Here is the relevant code -
private void CopyFolderRecursive(
SPFolder folder, SPWeb web, SPList sourceList,
string destinationUrl, SPList destinationList)
{
SPQuery qry = new SPQuery();
qry.Folder = folder;
SPListItemCollection ic = sourceList.GetItems(qry);
SPListItem destinationItem = null;
foreach (SPListItem sourceItem in ic)
{
if (sourceItem.FileSystemObjectType != SPFileSystemObjectType.Folder)
{
destinationItem = destinationList.Items.Add(
destinationUrl + "/" + folder.Url,
sourceItem.FileSystemObjectType);
foreach (SPField destinationField in destinationList.Fields)
{
if ((!destinationField.ReadOnlyField) &&
(destinationField.Type != SPFieldType.Attachments))
{
destinationItem[destinationField.Title] =
sourceItem[destinationField.Title];
}
}
destinationItem.Update();
}
else
{
destinationItem = destinationList.Items.Add(
destinationUrl + "/" + folder.Url,
SPFileSystemObjectType.Folder);
destinationItem["Title"] = sourceItem.Title;
destinationItem.Update();
}
if (sourceItem.Folder != null)
CopyFolderRecursive(
sourceItem.Folder, web, sourceList,
destinationUrl, destinationList);
}
}
The above can be called with a single line of code that looks like this -
CopyFolderRecursive(
sourceList.RootFolder, sourceWeb, sourceList,
destinationSiteURL, destinationList);
As you can see above, I am recursively going over the folder structure, and copying everything I see. If I run into a folder, viz.
if (sourceItem.FileSystemObjectType != SPFileSystemObjectType.Folder)
I create a folder using the following code -
destinationItem = destinationList.Items.Add(
destinationUrl + "/" + folder.Url,
SPFileSystemObjectType.Folder);
destinationItem["Title"] = sourceItem.Title;
destinationItem.Update();
Otherwise, I simply copy over the item field by field -
destinationItem = destinationList.Items.Add(
destinationUrl + "/" + folder.Url,
sourceItem.FileSystemObjectType);
foreach (SPField destinationField in destinationList.Fields)
{
if ((!destinationField.ReadOnlyField) &&
(destinationField.Type != SPFieldType.Attachments))
{
destinationItem[destinationField.Title] =
sourceItem[destinationField.Title];
}
}
destinationItem.Update();
This peice of code can be insanely useful in time of need :-).
Enjoy!
On
3/11/2008 7:10:26 PM
walid
said ..
hi,
if i would like to use this code to copy listItem from tasks to another list that i have created
where i should insert this code.
please help me i am Beginner on WF.
Thanks
|
On
5/12/2008 12:38:42 PM
Brajendu Das
said ..
Hi
I need to Use BizTalk Server for this pupose.Any Help...?
|
On
6/25/2008 2:53:30 PM
mich
said ..
Can I get a sample copy emailed to me of your small C# app that allows you to copy one list with folders and files to another list with simlar structure.
I see the code but not to fluent in c# to build it out but would liek to get this small app code, please willing to donate little something if it works for both wss 2.0 and wss 3.0?
|
On
7/17/2008 5:55:18 AM
ich
said ..
I have problems with the the File Ref. I always get an error at destinationItem.Update()
|
On
9/2/2008 4:20:15 PM
Bill
said ..
I tried out your code from above and gotten a couple of errors when executing. Do you have a recent update. Basically I am trying to use it with a document library which needs to be handled differently than a typical list.
|
On
10/29/2009 9:20:24 AM
John
said ..
Very nice, thank you!
|
On
1/14/2010 9:49:56 PM
Julie
said ..
I would like to know it also copies list items' version history...
|
On
4/21/2010 6:42:53 AM
oguer
said ..
The same in VB.NET :
Private Sub CopyFolderRecursive(ByVal folder As SPFolder, ByVal web As SPWeb, ByVal sourceList As SPList, ByVal destinationUrl As String, ByVal destinationList As SPList)
Dim qry As SPQuery = New SPQuery()
qry.Folder = folder
Dim ic As SPListItemCollection = sourceList.GetItems(qry)
Dim destinationItem As SPListItem = Nothing
For Each sourceItem As SPListItem In ic
If (sourceItem.FileSystemObjectType <> SPFileSystemObjectType.Folder) Then
destinationItem = destinationList.Items.Add(destinationUrl + "/" + folder.Url, sourceItem.FileSystemObjectType)
For Each destinationField As SPField In destinationList.Fields
If Not destinationField.ReadOnlyField And destinationField.Type <> SPFieldType.Attachments Then
destinationItem(destinationField.Title) = sourceItem(destinationField.Title)
Console.WriteLine(destinationField.Title)
End If
Next
destinationItem.Update()
Else
destinationItem = destinationList.Items.Add(destinationUrl + "/" + folder.Url, SPFileSystemObjectType.Folder)
destinationItem("Title") = sourceItem.Title
destinationItem.Update()
End If
If sourceItem.Folder Is Nothing Then
CopyFolderRecursive(sourceItem.Folder, web, sourceList, destinationUrl, destinationList)
End If
Next
End Sub
|
On
11/8/2010 9:48:52 PM
Steve Stacel
said ..
Hi,
Why do you need to copy a folder if you are copying a list item?
If I wanted to tailor this just to copy a list should I remove the folder code?
Thanks
Steve
|
On
12/1/2010 9:34:27 PM
Suni Shrestha
said ..
Hi,
I tried creating a webpart to copy the folder structure recursively with the same logic and it threw ma an error saying fileref not found could you please help me with that.
Thank you so much
Regards
suni
|
On
12/1/2010 9:34:43 PM
Suni Shrestha
said ..
Hi,
I tried creating a webpart to copy the folder structure recursively with the same logic and it threw ma an error saying fileref not found could you please help me with that.
Thank you so much
Regards
suni
|
On
1/29/2011 5:55:46 PM
tico
said ..
Hi,
how do you link this code to a content type
so a folder structure in a (template) library is copied into a new (custom) folder
|
On
2/11/2011 9:55:16 PM
Snehal H.Rana
said ..
How do I make this code into Custom activity for SharePoint Designer 2010?
|