I thought there was a simple method like MoveTo or CopyTo to move one listitem to another sharepoint list. MoveTo doesn’t exist and CopyTo didn’t behave like expected.
So I started looking on the internet and what I found was that the SPImport and SPExport classes did the job. There is a 6-part writeup at http://blogs.technet.com/stefan_gossner/archive/2007/08/29/deep-dive-into-the-sharepoint-content-deployment-and-migration-api-part-2.aspx. This however is an overkill for what I tried to accomplish.
Finally I made a function that copies all the fields and attachments one by one to another listitem.
Here is the code:
private void moveListItem(String destinationListName) { //creating new listitem at the destination list SPWeb web = workflowProperties.Web; SPList destList = web.Lists[destinationListName]; SPListItem destListItem = destList.Items.Add(); SPListItem sourceListItem = workflowProperties.Item; //transfer fields from sourceListItem to destListItem foreach (SPField field in sourceListItem.Fields) { if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments") { if (destListItem.Fields.ContainsField(field.InternalName)) { destListItem[field.InternalName] = sourceListItem[field.InternalName]; } } } //move all attachments one by one to the new listitem SPAttachmentCollection attColl = sourceListItem.Attachments; foreach (string fileName in attColl) { SPFile attFile = sourceListItem.ParentList.ParentWeb.GetFile(sourceListItem.Attachments.UrlPrefix + fileName); StreamReader fsReader = new StreamReader(attFile.OpenBinaryStream()); Stream fStream = fsReader.BaseStream; byte[] contents = new byte[fStream.Length]; fStream.Read(contents, 0, (int)fStream.Length); fStream.Close(); fStream.Dispose(); destListItem.Attachments.Add(fileName, contents); } destListItem.Update(); sourceListItem.Delete(); }
private void moveListItem(String destinationListName){//creating new listitem at the destination listSPWeb web = workflowProperties.Web;SPList destList = web.Lists[destinationListName];SPListItem destListItem = destList.Items.Add();SPListItem sourceListItem = workflowProperties.Item;//transfer fields from sourceListItem to destListItemforeach (SPField field in sourceListItem.Fields){if (!field.ReadOnlyField && !field.Hidden && field.InternalName != “Attachments”){if (destListItem.Fields.ContainsField(field.InternalName)){destListItem[field.InternalName] = sourceListItem[field.InternalName];}}}//move all attachments one by one to the new listitemSPAttachmentCollection attColl = sourceListItem.Attachments;foreach (string fileName in attColl){SPFile attFile = sourceListItem.ParentList.ParentWeb.GetFile(sourceListItem.Attachments.UrlPrefix + fileName);StreamReader fsReader = new StreamReader(attFile.OpenBinaryStream());Stream fStream = fsReader.BaseStream;byte[] contents = new byte[fStream.Length];fStream.Read(contents, 0, (int)fStream.Length);fStream.Close();fStream.Dispose();destListItem.Attachments.Add(fileName, contents);}destListItem.Update();sourceListItem.Delete();}