Move listitem from one sharepoint list to another

June 22nd, 2009 | Posted by Jeffrey Paarhuis in Developing

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 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();
}




You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses

  • guvencg says:

    Hi Jeff,

    Thanks for your article. In my case I’d like to move the ID of list item as well. Due to not having a move option while defining workflow in share point designer, I copied and deleted. Therefore I lost my ID :s

    I’ll be appreciated if you comment on this.

    Thanks.
    Guvenc

    • jeffreypaarhuis says:

      Hey, thanks for the comment, it’s a while back so I had to jeffrey.memory.flush() and jeffrey.memory.Write(oldArticle).

      Did the shit already hit the fan? Or are you looking for a way to deal with this in the future? In the first case, you can consult the recycle bin of the site and of central admin. In the last case it would be good to know where you do need the id´s for?



Leave a Reply to jeffreypaarhuis Cancel reply

%d bloggers like this: