当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 将附件从一个列表复制到另一个列表

作者:精品下载站 日期:2024-12-14 21:07:40 浏览:16 分类:玩电脑

SharePoint Online:使用 PowerShell 将附件从一个列表复制到另一个列表


要求:将附件从 SharePoint Online 中的一个列表复制到另一个列表。

PowerShell 将附件复制到 SharePoint Online 中的另一个列表:

我需要在 SharePoint Online 列表之间复制附件。 (仅限附件 - 不列出项目!)。该脚本从源列表中获取附件,根据“映射列”值搜索匹配的列表项,并将列表附件附加到目标列表项。


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

Function Copy-Attachments()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $SourceListName,
        [Parameter(Mandatory=$true)] [string] $TargetListName,
        [Parameter(Mandatory=$false)] [string] $MappingColumn="Title"
    )    
    Try {
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the Source List & Target Lists
    $SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
    $TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)
    
    #Get All Items
    $SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
    $TargetListItems = $TargetList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
    $Ctx.Load($SourceListItems)
    $Ctx.Load($TargetListItems)
    $Ctx.ExecuteQuery()

    #Iterate through each list item from source
    ForEach($SourceItem in $SourceListItems)
    {
        #Get All attachments from the List Item
        $AttachmentsColl = $SourceItem.AttachmentFiles
        $Ctx.Load($AttachmentsColl)
        $Ctx.ExecuteQuery()

        #Get Matching List item in the target list
        $ListItem = $TargetListItems | Where { $_[$MappingColumn] -eq $SourceItem[$MappingColumn]}
        if($ListItem -ne $null)
        {
            #Get attachment for each list item
            ForEach($Attachment in $AttachmentsColl)
            {
                Write-host "Copying attachment '$($Attachment.FileName)' from Item ID '$($SourceItem.ID)'"
                #Get attachment File from Source List Item
                $File = $Ctx.Web.GetFileByServerRelativeUrl($Attachment.ServerRelativeUrl)
                $Ctx.Load($File)
                $Ctx.ExecuteQuery()

                #Get the Source File Content
                $FileContent = $File.OpenBinaryStream()
                $Ctx.ExecuteQuery()
                $Buffer = New-Object Byte[]($File.length)
                $BytesRead = $FileContent.Value.Read($Buffer, 0, $Buffer.Length)
                $MemoryStream = New-Object -TypeName System.IO.MemoryStream(,$Buffer)

                #Add Attachment to Target List Item
                $AttachmentCreation = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
                $AttachmentCreation.ContentStream = $MemoryStream
                $AttachmentCreation.FileName = $Attachment.FileName 
                [void]$ListItem.AttachmentFiles.Add($AttachmentCreation)
                $Ctx.ExecuteQuery()
                $MemoryStream.Close()                
            }
       }
    }

    write-host  -f Green "List Attachments Copied from '$SourceListName' to '$TargetListName' !"
    }
    Catch {
        write-host -f Red "Error Copying List Attachments!" $_.Exception.Message
    } 
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$SourceListName="Projects"
$TargetListName="Project Temp"

#Call the function to copy list items
Copy-Attachments -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName 

PnP PowerShell 在列表项之间复制附件

以下是在 SharePoint Online 中的列表项之间复制附件的 PnP PowerShell 方法:


#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Config"

#Function to copy attachments between list items
Function Copy-SPOAttachments($SourceItem, $TargetItem)
{
    Try {  
        #Get All Attachments from Source
        $Attachments = Get-PnPProperty -ClientObject $SourceItem -Property "AttachmentFiles"
        $Attachments | ForEach-Object {
        #Download the Attachment to Temp
        $File  = Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $env:TEMP -AsFile -force

        #Add Attachment to Target List Item
        $FileStream = New-Object IO.FileStream(($env:TEMP+"\"+$_.FileName),[System.IO.FileMode]::Open)  
        $AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
        $AttachmentInfo.FileName = $_.FileName
        $AttachmentInfo.ContentStream = $FileStream
        $AttachFile = $TargetItem.AttachmentFiles.add($AttachmentInfo)
        $Ctx.ExecuteQuery()    
    
        #Delete the Temporary File
        Remove-Item -Path $env:TEMP$($_.FileName) -Force
        }
    }
    Catch {
        write-host -f Red "Error Copying Attachments:" $_.Exception.Message
    }
}

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential)
$Ctx=Get-PnPContext

#Get Source and Target List Items
$SourceItem = Get-PnPListItem -List $ListName -Id 1
$TargetItem = Get-PnPListItem -List $ListName -Id 4

#Call the function to copy attachments between list items
Copy-SPOAttachments $SourceItem $TargetItem

如果要将列表附件复制到文档库,请使用:SharePoint Online:将附件从列表复制到文档库

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯