SharePoint Copy List Items in a Generic Way

In this article we can explore the scenario of copying List Items in a Generic Way.

The generic way provides a solution in such a way that:

  • Requires no Hard Coding of List Columns
  • Ensures Read Only columns are not updated in the destination list
  • It provides an easier way to create duplicate items

Source List

For our example the following is the source list of type Contacts.

Image 1.jpg

Destination List

For the time being we are using the same source list as the destination list. This will create duplicate items in the Contacts list.

Application

Create a new Console Application, change the Target Platform to Any CPU and add the following code in the Program.cs file Main() method.

static void Main(string[] args)

{

    using (SPSite site = new SPSite("http://localhost"))

    {

        using (SPWeb web = site.OpenWeb())

        {

            CopyItems(web, "Contacts", "Contacts");

        }

    }

}

 

private static void CopyItems(SPWeb web, string sourceListName, string destListName)

{

    SPList sourceList = web.Lists[sourceListName];

    SPList destList = web.Lists[destListName];

 

    foreach (SPListItem item in sourceList.Items)

        CopyItem(item, sourceList);

}

 

private static void CopyItem(SPListItem item, SPList destList)

{

    SPListItem newItem = destList.Items.Add();

 

    for (int i = 0; i < item.Fields.Count; i++)

        if ((!newItem.Fields[i].ReadOnlyField) && (newItem.Fields[i].InternalName != "Attachments"))

            newItem[newItem.Fields[i].InternalName] = item[newItem.Fields[i].InternalName];

 

    newItem.Update();

}

Code Explained

The main method connects to the SharePoint site and invokes the CopyItems method. In the method the source and destination list names are passed.

CopyItems(web, "Contacts", "Contacts");

Inside the CopyItems method each SPList objects are constructed using the list names. Then using a foreach loop the items from a source list are fetched.

Each item is passed to the CopyItem method. Inside this method, each field of source item is iterated and copied to the destination item. The Internal Name of the field is used to avoid field order variation problems.

Each list contains a field named Attachment which will be throwing an exception while assigning. We are explicitly checking and removing this field.

Execution

On executing the application, we will see that a duplicate item is created in the Contacts list. (Please ensure you have at least one item in the source list).

Image 2.jpg

Executing multiple times will create duplicate numbers of items.

This concludes our article on copying list items in a generic way. I hope you enjoyed the code.

References

http://tinyurl.com/sp2010-spfld

Summary

In this article we have explored the code for copying items in a generic way. In the real life scenario this should help as an alternative to Save List as Template feature, creating duplicate list items.

The source code is attached with the article. I will be integrating this functionality to our Squadron tool for SharePoint.

Up Next
    Ebook Download
    View all
    Learn
    View all