How to copy SPListitem from one SPList to Another SPList

Azra picture Azra · Jul 17, 2009 · Viewed 20k times · Source

I have requirement to copy items from one SPList to another,

Here is the code which is not working:

public void CopyList(SPList src)
{
    //Copy items from source List to Destination List
    foreach (SPListItem item in src.Items)
    {
        if(isUnique(item.UniqueId))
        {
            foreach (SPField field in src.Fields)
            {
               try
               {
                    if (!field.ReadOnlyField)
                        newDestItem = DestinationList.Items.Add();
                    newDestItem[field.Id] = item[field.Id];
                    newDestItem.Update();
               }
               catch (Exception ex)
               {
                   ex.ToString();
               }
            }
            //newDestItem["wrkspace"] = src.ParentWeb.Name;
            // newDestItem.Update();
        }
        DestinationList.Update();  
    }
    // DestinationList.Update();
}

Answer

Rob Windsor picture Rob Windsor · Jul 17, 2009

The SPListItem type has a CopyTo method that will do what you want.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx