Thursday, May 27, 2010

Copy Images or Files from One Site to Another

//Get the context of the Image site
SPSite DestSite = new SPSite("Site URL");
//Access the folder under Images
SPFileCollection DestImages = DestSite.RootWeb.Folders["Images"].Files;
// Get the current site context
SPSite SourceSite = SPContext.Current.Site;
// Access the Provisioned site Webparts folder under _catalogs
SPFileCollection SourceImages = SourceSite.RootWeb.Folders["Images"].Files;
foreach (SPFile DestImage in DestImages)
{
// Load the properties of the existing file
byte[] binaryData = DestImage.OpenBinary();
// Copy the file
SPFile copiedFile = SourceImages.Add(DestImage.Name, binaryData, true);
copiedFile.Update();
}

No comments: