tags:

views:

5400

answers:

2

What's the best way to move a document from one doc library to another? I don't care about version history or preserving CreatedBy and ModifiedBy metadata...

SPList lib1 = (SPDocumentLibrary) web.Lists["lib1"];
SPList lib2 = (SPDocumentLibrary) web.Lists["lib2"];
SPItem item1 = lib1.Items[0];
//insert code to move item1 to lib2

I'm currently looking at SPItem.MoveTo() but wonder if anyone already solved this problem and has some advice.
Thanks in advance.

+1  A: 

Got it:

SPList lib1 = (SPDocumentLibrary) web.Lists["lib1"];
SPList lib2 = (SPDocumentLibrary) web.Lists["lib2"];
SPListItem item1 = lib1.Items[0];
byte[] fileBytes = item1.File.OpenBinary();
string destUrl = lib2.RootFolder.Url + "/" + item1.File.Name;
SPFile destFile = lib2.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);
vitule
... and then I delete item1 to satisfy the "move to" rather than "copy to" requirement.
vitule
A: 

MoveTo() and CopyTo() both seem to work fine within Visual Studio 2008 sequential workflow. But neither do meta-data or version history. :-(