Saturday, April 04, 2009

Copy Document Librarys between sites, No Metadata

Hi,

This method help you moving files and folders in Document Librarys between sites without metadata.

This method only works on same Server, external server you need to call webServices "Lists.asmx"

"Remeber this is only a example you can do a lot better and a lot more" :) 

Hope you like...

Here a nice example:

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 (SPFile file in docLibrary.RootFolder.Files)
{
Desweb.Files.Add(file.Url, file.OpenBinary(), true);
}
}
}
foreach (SPFile file in 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 (SPFile file in subFolder.Files)
{
web.Files.Add(file.Url, file.OpenBinary(), true);
}
CopySubFolders(subFolder);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


hope this help :)....

3 comments:

Jeroen Van Bastelaere said...

You can also try using the default sharepoint copyservice, this includes everything (but no versions)

If you also need versioning and cross webapp moving: SPImport and SPExport


Regards,
jeroen

Tecnologias .Net CRM SHAREPOINT said...

Yes I know, but have time and play a litle :), gonna update because this method cannot be used on diferent servers

jane bush said...

Nice informative post... thanks for sharing..

Regards,
Jane
Custom Sharepoint Development