SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Copy files in sites and site collections

Recently, I need to write a utility tools that handles moving documents (or aspx pages in a site) within the same site or accross site collections. The WSS 3.0 object model provides classes that would do that easily in code.

I use the methods in SPFile class to do a copy/move operation since the source and destination locations are in the same wss site.

SPSite site = new SPSite(txtWebUrl.Text);
SPWeb web = site.OpenWeb(relativeUrl);

SPFolder srcFolder = web.GetFolder(srcFolderName);
SPFileCollection collFiles = srcFolder.Files;

collFiles[index].CopyTo(destFolderName + "/" + collFiles[index].Name);

web.dispose();
site.dispose();

To copy files across site collection, I use the SPFileCollection class:

SPFileCollection targetPages = target.Files;
SPFileCollection sourcePages = source.Files; 
 foreach (SPFile page in sourcePages)
{   
      try
   
    
{
       
           
byte[] binFile = page.OpenBinary();
       
           
SPFile newPage = targetPages.Add(targetSiteUrl + "/" + page.Name, binFile);
   
     
}
   
     
catch (Exception ex)
   
    
{
            
MessageBox.Show(ex.Message);
      }
}

Posted 04-18-2008 3:26 PM by jleung

Comments

Links (4/20/2008) « Steve Pietrek - Everything SharePoint wrote Links (4/20/2008) « Steve Pietrek - Everything SharePoint
on 04-21-2008 10:35 AM

Pingback from  Links (4/20/2008) « Steve Pietrek - Everything SharePoint

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.