Udostępnij za pośrednictwem


The SPListItem.CopyTo() method doesn't seem to work on a custom list

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, Dorrit provides source code you can cut and paste, look for the section in italics near the end of his post: https://www.communardo.de/techblog/2008/01/08/sharepoint-listenelement-splistitem-in-eine-andere-liste-kopieren/

SPListItem offers a CopyTo(destinationUrl) method that doesn't work with a custom list, insteadyou get the exception: "Source item cannot be found. Verify that the item exists and that you have permission to read it."

Dorrit wrote a method that 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.