Copy a List Item in SharePoint from one list to another

We had a requirement for archiving a list in SharePoint.  The list had grown out of control, in excess of (300k items+).  To archive the list we used the console app and created a set of archive lists and grouped them by quarter. We then moved the SharePoint ListItems to the new list.

The below code should copy over the SharePoint ListItem (along with their attachments) to the new list.  The source SharePoint ListItem can then be deleted.

ListItem Code

private SPListItem CopyItem(SPListItem sourceItem, string destinationListName)
        {
            //Copy sourceItem to destinationList
            SPList destinationList = sourceItem.Web.Lists[destinationListName];
            SPListItem targetItem = destinationList.Items.Add();
            foreach (SPField f in sourceItem.Fields)
            {
                //Copy all except attachments.
                if (!f.ReadOnlyField && f.InternalName != "Attachments"
                    && null != sourceItem[f.InternalName])
                {
                    targetItem[f.InternalName] = sourceItem[f.InternalName];
                }
            }
            //Copy attachments
            foreach (string fileName in sourceItem.Attachments)
            {
                SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
                byte[] imageData = file.OpenBinary();
                targetItem.Attachments.Add(fileName, imageData);
            }
            targetItem.SystemUpdate();
            targetItem.BreakRoleInheritance(false);
            foreach(SPRoleAssignment roleAssignment in sourceItem.RoleAssignments)
            {
                targetItem.RoleAssignments.Add(roleAssignment);
            }
            return targetItem;
        }

If you would be interested in engaging an experienced SharePoint consultant, call us on 1300 797 888.