Share via


Sharepoint: Copy/Move list item from one list to another

Hi Everyone,

The below 2 code snippets will help to copy / move the list item from one list to another. 

This code is to copy the list item without modifying any thing in the existing list.

01.private void CopyItem(SPListItem item, SPList destList)
02.        {
03.            SPListItem newItem = destList.Items.Add();
04.            foreach (SPField field in item.Fields)
05.            {
06.                if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments")
07.                {
08.                    if (newItem.Fields.ContainsField(field.InternalName))
09.                        newItem[field.InternalName] = item[field.InternalName];
10.                }
11.            }
12.            newItem.Update();
13.        }

This code will copy the list item to the new list and delete the same in the existing list. Like this you can achieve moving the list item function. 

01.private void MoveItem(SPListItem item, SPList destList)
02.        {
03.            SPListItem newItem = destList.Items.Add();
04.            foreach (SPField field in item.Fields)
05.            {
06.                if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments")
07.                {
08.                    if (newItem.Fields.ContainsField(field.InternalName))
09.                        newItem[field.InternalName] = item[field.InternalName];
10.              item.delete();
11.              item.update()
12.                }
13.            }
14.            newItem.Update();
15.        }

For any queries reach me out through 
www.linkedin.com/in/sasisprite