Monday, 30 November 2015

Copy files and Folders in SharePoint from One Site to anonther Site

Below PowerShell Script to copy files and folders from one Site to anonther through powershell

cls

asnp "*sh*"

write-Host "Please Provide the Source web site url and Desstination url"-foregroundcolor white -backgroundcolor blue
##
$SourceWebURL = read-host "Enter Source WebURL:"
$DestinationWebURL = read-host "Enter Destination WebURL:"
##
write-Host "Please Provide the Source library title and Destination library title"-foregroundcolor white -backgroundcolor blue
##
$SourceLibraryTitle = read-host "Enter Source LibraryTitle:"
$DestinationLibraryTitle = read-host "Enter Destination LibraryTitle:"
##
write-output "Started...";
##
$sWeb = Get-SPWeb $SourceWebURL
$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle}
$dWeb = Get-SPWeb $DestinationWebURL
$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle}

$AllFolders = $sList.Folders
$RootFolder = $sList.RootFolder
$RootItems = $RootFolder.files

foreach($RootItem in $RootItems)
{
    $sBytes = $RootItem.OpenBinary()
    $dFile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true)

    $AllFields = $RootItem.Item.Fields | ? {!($_.sealed)}

    foreach($Field in $AllFields)
    {
        if($RootItem.Properties[$Field.Title])
        {
            if(!($dFile.Properties[$Field.title]))
            {
                $dFile.AddProperty($Field.Title, $RootItem.Properties[$Field.Title])
            }
            else
            {
                $dFile.Properties[$Field.Title] = $RootItem.Properties[$Field.Title]
            }
        }
    }
    $dFile.Update()
}

foreach($Folder in $AllFolders)
{
    #Remove-Variable ParentFolderURL
        $varExists = Get-Variable ParentFolderURL -ErrorAction SilentlyContinue

    if($varExists -ne $null){

           Remove-Variable ParentFolderURL  }
    $i = 0
    
    $FolderURL = $Folder.url.Split("/")
        
    while($i -lt ($FolderURL.count-1))
    {
    $ParentFolderURL = "$ParentFolderURL/" + $FolderURL[$i]
    $i++
    }
    
    $CurrentFolder = $dList.Folders | ? {$_.url -eq $ParentFolderURL.substring(1)}
    if(!($CurrentFolder.Folders | ? {$_.name -eq $Folder.Name}))
    {
        $NewFolder = $dlist.Folders.Add(("$DestinationWebURL" + $ParentFolderURL), [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.name)
        $NewFolder.update()
    }
    else
    {
        $NewFolder = $dList.Folders | ? {$_.name -eq $Folder.Name}
    }
    $AllFiles = $sList.Items
    $sItems = $Folder.folder.Files
    
    if($Folder.Folder.Files.count -gt 0)
    {
        foreach($item in $sItems)
        {
            
            #$Relative = ($Item.ServerRelativeUrl).substring(1)
            $Relative = ($Item.Url)
            $TargetItem = $AllFiles | ? {$_.URL -eq $Relative}
            $sBytes = $TargetItem.File.OpenBinary()
            $dFile = $Newfolder.Folder.Files.Add($TargetItem.Name, $sBytes, $true)
            $AllFields = $TargetItem.Fields | ? {!($_.sealed)}
            
            foreach($Field in $AllFields)
            {
                if($TargetItem.Properties[$Field.Title])
                {
                    if(!($dFile.Properties[$Field.title]))
                    {
                        $dFile.AddProperty($Field.Title, $TargetItem.Properties[$Field.Title])
                    }
                    else
                    {
                        $dFile.Properties[$Field.Title] = $TargetItem.Properties[$Field.Title]
                    }
                }
            }
            $dFile.Update()
        }
    }
}
Write-Host "All Files are moved successfully" -foregroundcolor green

No comments:

Post a Comment