How to Copy Document librarys Between websites in SharePoint

Posted By  André Lage On 15 Jul 2010 08:07:25
emailbookmarkadd commentsprint
No of Views:1137
Bookmarked:0 times
Votes:0 times

Introduction

This method help you moving files and folders in Document library's Between sites without metadata.
This method only works on Same Server, external server You Need to call webServices "Lists.asmx".Remeber this is the only example you can-do a lot better and a lot more.

Code snippet

try(
using (SPSite site = new SPSite ("http:// [site]"))
(
using (SPWeb web = site.OpenWeb ())
(
SPDocumentLibrary docLibrary = (SPDocumentLibrary) web.Lists ["Shared Documents"];
SPFolder root = docLibrary.RootFolder; 
using (SPSite DesSite = new SPSite ("[DestinySite]"))
(
using (SPWeb Desweb DesSite.OpenWeb = ())
(
foreach (file in SPFile docLibrary.RootFolder.Files)
(
Desweb.Files.Add (file.Url, file.OpenBinary (), true);
)
)
)
foreach (file in SPFile root.Files)
(
web.Files.Add (file.Url, file.OpenBinary (), true);
)
CopySubFolders (root); 
)
)

)
catch (Exception ex)
(
MessageBox.Show (ex.Message);
)



private void CopySubFolders (SPFolder folder)
(
try(
using (SPSite site = new SPSite ("http:// [DestinySite ]/"))
(
using (SPWeb web = site.OpenWeb ())
(
SPFolderCollection subfolders = folder.SubFolders;
foreach (SPFolder subfolder in subfolders)
(
web.Folders.Add (subFolder.Url);
foreach (file in SPFile subFolder.Files)
(
web.Files.Add (file.Url, file.OpenBinary (), true);
)
CopySubFolders (subfolder);
)
)
)
)
catch (Exception ex)
(
MessageBox.Show (ex.Message);
)
)

 Hope this help.

Sign Up to vote for this article
Other popular Tips/Tricks
Comments
By:you = idietDate Of Posted:2/9/2011 7:49:46 AM
U are an idiet
Brackets ?! WTF http://piv.pivpiv.dk/
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top