Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

Copying over a SharePoint list from source site to destination site

Posted on 5/21/2007 @ 9:42 PM in #Sharepoint | 5 comments | 9109 views

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.

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.