Sunday 23 October 2011

Powershell script to copy documents between sites in Sharepoint 2007 or Sharepoint 2010

Hi,

I have observed that now a days a very common and cumbersome task for the administrators is to copy documents between sites. I have observed that workflows are used to do the same. To provide more flexibility I have written the following script. This scrip is executed in two modes in the first mode it copies all the files from source folder and copied it to the destination folder. These folders can be the document library or the folders within them. When you will be asked for the folders URL you need to provide that in the following format.
http://site/library or http://site/library/Foldername . In the second way you can choose to copy one file among all files in the source folder to copy to destination folder. This works in MOSS 2007 as well as in SPS 2010.


param([switch]$help)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

function GetHelp() {


$HelpText = @"

DESCRIPTION:
This script 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.
"@
$HelpText

}

function RahulCopyDocumentsInSharepoint() {

write-host "Hello, This tool will copy the the files between the folders specified. It will overwrite the file if it exists at the destination location"
        write-host "Please enter the URL of the source folder"
        $sourceURL = read-host
        write-host "Please enter the URL of the destination folder"
        $destinationURL = read-host
        write-host "do you want to copy a specific file?if yes then type y or else type anything else"
        $fileDecision = read-host
        $cFile = [String]::Empty
        if($fileDecision -eq "y")
                {
                    write-host "Please enter the filename with extension of the file"
                    $cFile = read-host
                }
        $sourceSite = New-Object Microsoft.SharePoint.SPSite($sourceURL)
        $sourceWeb = $sourceSite.OpenWeb()
        $destinationSite = New-Object Microsoft.SharePoint.SPSite($destinationURL)
        $destinationWeb = $destinationSite.OpenWeb()
        $sList = [Microsoft.Sharepoint.SPFolder]$sourceWeb.GetFolder($sourceURL)
        $dList = [Microsoft.Sharepoint.SPFolder]$destinationWeb.GetFolder($destinationURL)
        $dLibrary = $destinationWeb.Lists[$dList.ContainingDocumentLibrary]
        $files = $sList.Files
        if ($fileDecision -ne "y")
        {
        foreach ($ctFile in $files)
        {
        $sbytes = $ctFile.OpenBinary()
        $dListFiles = $dList.Files
        foreach ($dListFile in $dListFiles)
        {
        if($dListFile.Name.Equals($ctFile.Name))
         {
         $dListFile.CheckOut()
         }
         }
        $dFileName = $dList.Files.Add($ctFile.Name, $sbytes, $true)
        if ($dFileName.CheckOutStatus -ne [Microsoft.Sharepoint.SPFile]::SPCheckOutStatus::None)
           {
           $dFileName.CheckIn("Checking in")
            }
            }
            }
            else
            {
            $desFile = $sList.Files[$sourceURL +"/" + $cFile]
            $sbytes = $desFile.OpenBinary()
             $dListFiles = $dList.Files
            foreach ($dListFile in $dListFiles)
             {
            if($dListFile.Name.Equals($desFile.Name))
               {
          $dListFile.CheckOut()
              }
                                        }
            $dFileName = $dList.Files.Add($desFile.Name, $sbytes, $true)
            if ($dFileName.CheckOutStatus -ne [Microsoft.Sharepoint.SPFile]::SPCheckOutStatus::None)
            {
          $dFileName.CheckIn("Checking in")
             }
            }
                                $destinationWeb.Update()
                                write-host "The operation completed successfully"
                               

$sourceSite.Dispose()
$sourceWeb.Dispose()
$destinationSite.Dispose()
$destinationWeb.Dispose()
       
}

if($help) { GetHelp; Continue }
else { RahulCopyDocumentsInSharepoint }

I hope this will help you out.

Thanks,
Rahul Rashu

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi,
      You need to provide y or anything else to copy folder or the specific file. have you done that.

      Thanks,
      Rahul Rashu

      Delete