FIX: Can't Move/Copy an SPListItem
Kirk Hofer

Microsoft Dynamics Vertical

Inetium can implement a CRM solution that fits your business needs.

Learn More >>

Inetium is the go to partner for Microsoft SharePoint consulting services.

Learn more >>

Syndication

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 03-16-2008 8:22 PM by khofer
Filed under:

Comments

Matt wrote re: FIX: Can't Move/Copy an SPListItem
on 04-01-2008 10:46 AM

Out of curiousity, are you moving a list item from one folder to another in the SAME LIST?

Because I'm having a difficult time trying to get this to work by moving items from one list, to another list. Doesn't seem to work.

Thanks for any input.

Ajay wrote re: FIX: Can't Move/Copy an SPListItem
on 05-05-2008 10:16 PM

Hey Mate,

I am using your code,, but cannot see the item in the new list.

It moves from the original list but does not appear in the list where it is moved to.

Though if I try to move again without setting overwrite == true overwrite it complains.. that item exits.

Can you please let me know if it appears in the new list.

My code is

foreach (SPListItem item in items)

{

SPFile f = site.GetFile(item.Url);

string url = myUrl + "/Lists/" + toDocumentLibrary.Title + "/" + f.Name;

f.CopyTo(url);

                   //f.CopyTo(url,true);

               }

Ajay wrote re: FIX: Can't Move/Copy an SPListItem
on 05-07-2008 8:37 PM

Hi Kirk,

When I do the moveto.. the item moves from the list but is not visible in the new list.

If I copy the same item in the list and do moveto again, then I get the error message that item already exists..

Can you please tell why is this happening.

Thanks

Ajay

Kirk wrote re: FIX: Can't Move/Copy an SPListItem
on 05-07-2008 9:03 PM

Sorry for the late responses on some of these (sorry Matt...site just told me I had comments).  This was only tested on the SAME list.  Haven't tried the CopyTo() with the SPFile.  Is it the same Site Collection or different?  In the first code bit Ajay, you mentioned CopyTo(), did you try MoveTo() first?  I know the move has some issues across SCs.

Kirk

Discovia wrote re: FIX: Can't Move/Copy an SPListItem
on 07-24-2008 1:57 PM

This sould come with a warning.  My items disappeared too.  Although they show up in the item count of the list; they are no longer visable.

John P wrote re: FIX: Can't Move/Copy an SPListItem
on 09-02-2008 7:45 AM

As a note, to make this scalable you should follow the procedure below.  I have only seen this work within the context of the same list, but you might be able to get it working with two identically configured lists.  

SPFile file = currentWeb.GetFile(itemToMove.Url);

file.MoveTo(newLocationUrl + "/" + itemToMove.ID.ToString() + "_.000");

                           itemToMove.Update();

Sivaram wrote re: FIX: Can't Move/Copy an SPListItem
on 10-02-2008 9:48 PM

The MoveTo command is not working between two identical list

                   SPFile fileitem = webApplication.Sites[0].RootWeb.GetFile(itemsp.Url);

                   string strDestinationPath= webApplication.Sites[0].RootWeb.Url + "/" + "Lists"+ "/" + "ReferralBackup" + "/" + itemsp.ID + "_.000";

                   fileitem.MoveTo(strDestinationPath);

                   itemsp.Update();

Chris Sammut wrote re: FIX: Can't Move/Copy an SPListItem
on 11-21-2008 5:22 AM

Hi,

I am trying to copy a calendar item to another calendar list on another site.

The Move to function is different to the Copyto in the sense that it removes the item in the current list.

In addition, I tried your method and it doesnt seem to copy the item to the destination url.

Any ideas appreciated.

thank you

Regards

Chris

Shared Points for SharePoint... wrote Copy listitems from one custom list to another, then move them into subfolders.
on 04-30-2009 2:03 AM

One problem I stumbled over the other day, was how to copy item from one custom SPlist to another. The

Inetium, LLC. Site Information