Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

SharePoint Copy List Items in a Generic Way

Posted by Paul on December 15, 2012


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

The generic way provides solution in such a way that:

· No Hard Coding of List Columns

· Ensures Read Only columns are not updated in Destination list

· It provides easier way of creating duplicate items

Source List

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

clip_image002

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 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 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.

clip_image004

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

Execution

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

clip_image006

Executing multiple times will create duplicate number 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s