SharePoint: programmatically move documents between document libraries

What is the best way to move a document from one document library to another? I don't need version history or saving 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 am currently looking at SPItem.MoveTo() , but am wondering if anyone has resolved this problem and got some advice. Thanks in advance.

+4
source share
2 answers

Got this:

 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*/); 
+5
source

MoveTo () and CopyTo () both seem to work fine in the sequential workflow of Visual Studio 2008. But both metadata and version history .: - (

0
source

All Articles