Translated solution to SPListItem CopyTo bug

Problem: The SPListItem.CopyTo() method doesn't seem to work on a custom list or custom properties. When called, CopyTo() returns the error "Source item cannot be found. Verify that the item exists and that you have permission to read it."

Solution: Dorrit Riemenschneider posted this solution in German, which is roughly translated below thanks to Babelfish and a little of my own interpretation. Dorrit provides source code you can cut and paste, look for the section in italics near the end of his post:

http://www.communardo.de/techblog/2008/01/08/sharepoint-listenelement-splistitem-in-eine-andere-liste-kopieren/

Translation: SPListItem offers a CopyTo(destinationUrl) method that doesn't work, at least in my case (a custom list with custom properties and attachments, maybe I expect too much of SharePoint). Instead I receive the exception: "Source item cannot be found. Verify that the item exists and that you have permission to read it." A quick Google search revealed that other people had the same problem but no solution. And so I bring you a method that works.

The method takes the SPListItem and destination list as parameters, and returns a reference to the new list item.

First the method adds an item at the target location. Then it steps through and copies each field. Note that we can't copy read-only fields, and attachments require special treatment. Once complete, the target is written with Update() and a reference to the destination element is returned.

Enjoy!  

No Comments