in

Kirk Hofer

March 2008 - Posts

  • FIX: Can't Move/Copy an SPListItem

    Problem:

    SPListItem.CopyTo() does not work.  You always get the error "Source item cannot be found.  Verify that the item exist and that you have permission to read it.".  Nice error…tells you nothing especially since you have a pointer to the item so obviously you can read it…duh. 

     

    Solution:

    I have seen several posts out there just looking through columns and creating a copy of the item by hand...yuck.  This was so easy, that it is taking me longer to write this blog....The key is the SPListItem.Url and the name it gives to the file with <ID>_.000

     

    1. Get the SPListItem.Url and then use this to get a pointer to the SPFile object.  Of course, you can't just hit the SPListItem.File since this is not a document library.

    2. Use the SPFile.MoveTo() method and you are done

     

    Example:

    SPListItem item = Web.Lists["Announcement"].GetItemById(5);
    SPFile file = Web.GetFile(item.Url);
    file.MoveTo("New location...with the ID_.000");
    

     

    Enjoy!

    Posted Mar 16 2008, 08:22 PM by khofer with 7 comment(s)
    Filed under:
Inetium, LLC. Disclaimer