0

a customer has two sharepoint online libraries on different sites and wants to migrate one into another. Using "copy to" would be no option for them, they would like to do it via a script.

They have already copied some files to the new site but not all. Therefore they would need a script to compare last edit dates on all files in the libraries and copy them if the edit dates of the files in the old library are more recent. If not, they should not be copied of course.

Is this a possible scenario in PowerShell? If yes, how?

0

"copy to" could be an option as this functionality could be implemented via Powershell. Please take a reference of below script:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Admin User Principal Name
$username = 'abc@xxx.onmicrosoft.com'

#Get Password as secure String
$password = ConvertTo-SecureString "password" -AsPlainText -Force

#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username , $password)
  
#Function to Move a File
Function Move-SPOFile([String]$SiteURL, [String]$SourceFileURL, [String]$TargetFileURL)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $credentials
      
        #sharepoint online powershell to move files
        $CopyMigrationOptions = New-Object Microsoft.SharePoint.Client.CopyMigrationOptions -Property @{"IgnoreVersionHistory"=$false;"IsMoveMode"=$false;"AllowSchemaMismatch"=$true} 
       
        $result = $Ctx.Site.CreateCopyJobs($SourceFileURL,$TargetFileURL,$CopyMigrationOptions)
        
        $Ctx.ExecuteQuery()
  
        Write-host -f Green "File Moved Successfully!"
    }
    Catch {
        write-host -f Red "Error Moving the File!" $_.Exception.Message
    }
}
  
#Set Config Parameters
$SiteURL="https://xxx.sharepoint.com/sites/s01"
$SourceFileURL=@("https://xxx.sharepoint.com/sites/s01/Mytestlib/folder01/testfile.chm")
$TargetFileURL="https://xxx.sharepoint.com/sites/s02/Shared Documents"
  
#Call the function to Move the File
Move-SPOFile $SiteURL $SourceFileURL $TargetFileURL

When you click 'copy to' on the UI, the behind request is:

..../_api/site/CreateCopyJobs

By digging above request, we contructe above cmdlets with CSOM. Below is the info about "CopyMigrationOptions":

<ComplexType Name="CopyMigrationOptions">
    <Property Name="AllowSchemaMismatch" Type="Edm.Boolean" Nullable="false"/>
    <Property Name="AllowSmallerVersionLimitOnDestination" Type="Edm.Boolean" Nullable="false"/>
    <Property Name="IgnoreVersionHistory" Type="Edm.Boolean" Nullable="false"/>
    <Property Name="IsMoveMode" Type="Edm.Boolean" Nullable="false"/>
    <Property Name="NameConflictBehavior" Type="Edm.Int32" Nullable="false"/>
</ComplexType>

As for getting item edit dates, you can achieve it throguh CSOM or PnP powershell:

$item=Get-PnPListItem -List "Documents"
$item["Modified"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.