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 :)....
Post a Comment