首页 > 解决方案 > Azure 自动化 - 将文件从一个 Sharepoint 站点复制到另一个

问题描述

我想创建一个工作流,自动将上传到 Sharepoint 站点的文件复制到另一个 SharePoint 站点(用于与客户交换文件)。因此,我创建了一个逻辑应用程序,该应用程序触发具有以下内容的 Runbook:

param(
    [Parameter (Mandatory = $true)][string]$FilePath,
    [Parameter (Mandatory = $true)][string]$FileName
)

$Client = "C:\Modules\User\Microsoft.SharePoint.Client\Microsoft.SharePoint.Client.dll"
$ClientRT = "C:\Modules\User\Microsoft.SharePoint.Client\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path $Client
Add-Type -Path $ClientRT

Creds = Get-AutomationPSCredential -Name "SharepointCreds"
 
#Set parameter values
$TargetSiteURL="https://domain.SharePoint.com/sites/site1"
$SourceSiteURL="https://domain.Sharepoint.com/sites/site2"
 
#Set Source and Destination File URL
$SourceFileURL="/sites/Sitename1/$Filepath/$FileName"
$TargetFileURL="/sites/Sitename2/$Filepath/$FileName"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName, $Creds.Password)
 
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
 
#Get the Source File
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceCtx, $SourceFileURL)
 

#Copy File to the Target location
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetCtx, $TargetFileURL, $FileInfo.Stream,$True)

无论如何,我一直收到以下错误:Cannot send a content-body with this verb-type

我该如何解决这个问题?有更好的方法吗?

标签: powershellsharepointazure-logic-appsazure-automationazure-runbook

解决方案


在 sharepoint 中,我们可以使用MoveCopyUtil 类在站点之间复制文件

例如:

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
    $Ctx.Credentials = $Credentials      
    #Copy the File
    $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
    $Overwrite = $True
    [Microsoft.SharePoint.Client.MoveCopyUtil]::CopyFile($Ctx, $SourceFileURL, $TargetFileURL, $Overwrite, $MoveCopyOpt)
    $Ctx.ExecuteQuery()

参考:https ://www.sharepointdiary.com/2017/02/sharepoint-online-copy-file-between-document-libraries-using-powershell.html#ixzz7CSvEyjEc


推荐阅读