SharePoint 2010: How to Copy Documents Between Sites in 2007 or 2010

SharePoint 2010: How to Copy Documents Between Sites in 2007 or 2010

I have received a request from a site administrator to provide a way to copy documents between sharepoint sites. The requirement was to copy the files between different site collections as well as as sites created in other web applications. This tool can be executed in two modes in the first mode it will copy all files from the source folder to the destination folder, in the second mode it will copy the specific file from the source folder to destination folder. This folder can be a document library or a folder inside the document library.
This will also check in the file so that it will be visible to authorized users. If the file already exists in the destination location this tool will overwrite the file. I have tested this in both Sharepoint 2007 and Sharepoint 2010.

I am sharing the code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace CopyDocumentLibrariesRashu
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Hello, This tool will copy the the files between the folders specified. It will overwrite the file if it exists at the destination location");
                Console.WriteLine("Please enter the URL of the source folder");
                string sourceURL = Console.ReadLine();
                Console.WriteLine("Please enter the URL of the destination folder");
                string destinationURL = Console.ReadLine();
                Console.WriteLine("do you want to copy a specific file?if yes then type y or else type anything else");
                String fileDecision = Console.ReadLine();
                String cFile = string.Empty;
                if(fileDecision == "y")
                {
                    Console.WriteLine("Please enter the filename with extension of the file");
                     cFile = Console.ReadLine();
                }
                using (SPSite sourceSite = new SPSite(sourceURL))
                {
                    using (SPWeb sourceWeb = sourceSite.OpenWeb())
                    {
                        using (SPSite destinationSite = new SPSite(destinationURL))
                        {
                            using (SPWeb destinationWeb = destinationSite.OpenWeb())
                            {
                                SPFolder sList = (SPFolder)sourceWeb.GetFolder(sourceURL);
                                SPFolder dList = (SPFolder)destinationWeb.GetFolder(destinationURL);
                                SPList dLibrary = destinationWeb.Lists[dList.ContainingDocumentLibrary];
                                SPFileCollection files = sList.Files;
                                if (fileDecision != "y")
                                {
                                    foreach (SPFile ctFile in files)
                                    {
                                        byte[] sbytes = ctFile.OpenBinary();
                                        SPFile dFileName = dList.Files.Add(ctFile.Name, sbytes, true);
                                        if (dFileName.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                                        {
                                            dFileName.CheckIn("Checking in");
                                        }
                                    }
                                }
                                else
                                {
                                    SPFile desFile = sList.Files[sourceURL +"/" + cFile];
                                    byte[] sbytes = desFile.OpenBinary();
                                    SPFile dFileName = dList.Files.Add(desFile.Name, sbytes, true);
                                    if (dFileName.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                                    {
                                        dFileName.CheckIn("Checking in");
                                    }
 
 
                                }
                                destinationWeb.Update();
                                Console.WriteLine("The operation completed successfully");
                                Console.ReadLine();
 
 
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        
     
    }
}

Leave a Comment
  • Please add 7 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Gokan Ozcifci edited Revision 5. Comment: Formatting  

  • Patris_70 edited Revision 3. Comment: deleted (en-US) from title

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

  • Ed Price - MSFT edited Revision 1. Comment: Title and tags

Page 1 of 1 (4 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Gokan Ozcifci edited Revision 5. Comment: Formatting  

  • Patris_70 edited Revision 3. Comment: deleted (en-US) from title

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

  • Ed Price - MSFT edited Revision 1. Comment: Title and tags

Page 1 of 1 (4 items)