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

[玩转系统] SharePoint Online:使用 PowerShell 在网站集之间复制列表项

作者:精品下载站 日期:2024-12-14 15:31:33 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 在网站集之间复制列表项


要求: 使用 PowerShell 在 SharePoint Online 中的网站集之间复制列表项。

[玩转系统] SharePoint Online:使用 PowerShell 在网站集之间复制列表项

PowerShell 在 SharePoint Online 网站集之间复制列表项

当您需要在 SharePoint Online 中的网站集之间复制列表项时,PowerShell 可以是一种快速而简单的方法。这篇博文将向您展示如何使用 PowerShell 将列表项从一个网站集复制到另一个网站集。如果您必须在两个不同的网站之间复制数据或想要备份数据,或者您需要将多个列表中的信息合并到一个位置,或者您希望从一个网站集迁移数据,则此功能会很有帮助到另一个。

只需相应设置参数并运行脚本即可。确保您的目标列表已使用相同的结构创建(通过从现有列表创建新列表或将列表保存为模板并创建新列表实例,以便内部名称保持不变!)。更多信息请参见:如何在 SharePoint Online 中复制列表?


#Function to copy attachments between list items
Function Copy-SPOAttachments()
{
    param
    (
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.ListItem] $SourceItem,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.ListItem] $DestinationItem
    )
    Try {
        #Get All Attachments from Source list items
        $Attachments = Get-PnPProperty -ClientObject $SourceItem -Property "AttachmentFiles" -Connection $SourceConn
        $Attachments | ForEach-Object {
            #Download the Attachment to Temp
            $File  = Get-PnPFile -Connection $SourceConn -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $Env:TEMP -AsFile -Force
            #Add Attachment to Destination 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 = $DestinationItem.AttachmentFiles.Add($AttachmentInfo)
            Invoke-PnPQuery -Connection $DestinationConn
      
            #Delete the Temporary File
            Remove-Item -Path $Env:TEMP$($_.FileName) -Force
        }
    }
    Catch {
        write-host -f Red "Error Copying Attachments:" $_.Exception.Message
    }
}

#Function to copy list items from one list to another
Function Copy-SPOListItems()
{
    param
    (
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.List] $SourceList,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.List] $DestinationList
    )
    Try {
        #Get All Items from the Source List in batches 
        Write-Progress -Activity "Reading Source..." -Status "Getting Items from Source List. Please wait..."
        $SourceListItems = Get-PnPListItem -List $SourceList -PageSize 500 -Connection $SourceConn
        $SourceListItemsCount= $SourceListItems.count
        Write-host "Total Number of Items Found:"$SourceListItemsCount      
  
        #Get fields to Update from the Source List - Skip Read only, hidden fields, content type and attachments
        $SourceListFields = Get-PnPField -List $SourceList -Connection $SourceConn | Where { (-Not ($_.ReadOnlyField)) -and (-Not ($_.Hidden)) -and ($_.InternalName -ne  "ContentType") -and ($_.InternalName -ne  "Attachments") }

        #Loop through each item in the source and Get column values, add them to Destination
        [int]$Counter = 1
        ForEach($SourceItem in $SourceListItems)
        {  
            $ItemValue = @{}
            #Map each field from source list to Destination list
            Foreach($SourceField in $SourceListFields)
            {
                #Check if the Field value is not Null
                If($SourceItem[$SourceField.InternalName] -ne $Null)
                {
                    #Handle Special Fields
                    $FieldType  = $SourceField.TypeAsString                    
  
                    If($FieldType -eq "User" -or $FieldType -eq "UserMulti") #People Picker Field
                    {
                        $PeoplePickerValues = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.Email}
                        $ItemValue.add($SourceField.InternalName,$PeoplePickerValues)
                    }
                    ElseIf($FieldType -eq "Lookup" -or $FieldType -eq "LookupMulti") # Lookup Field
                    {
                        $LookupIDs = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.LookupID.ToString()}
                        $ItemValue.add($SourceField.InternalName,$LookupIDs)
                    }
                    ElseIf($FieldType -eq "URL") #Hyperlink
                    {
                        $URL = $SourceItem[$SourceField.InternalName].URL
                        $Description  = $SourceItem[$SourceField.InternalName].Description
                        $ItemValue.add($SourceField.InternalName,"$URL, $Description")
                    }
                    ElseIf($FieldType -eq "TaxonomyFieldType" -or $FieldType -eq "TaxonomyFieldTypeMulti") #MMS
                    {
                        $TermGUIDs = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.TermGuid.ToString()}                    
                        $ItemValue.add($SourceField.InternalName,$TermGUIDs)
                    }
                    Else
                    {
                        #Get Source Field Value and add to Hashtable                        
                        $ItemValue.add($SourceField.InternalName,$SourceItem[$SourceField.InternalName])
                    }
                }
            }
            #Copy Created by, Modified by, Created, Modified Metadata values
            $ItemValue.add("Created", $SourceItem["Created"]);
            $ItemValue.add("Modified", $SourceItem["Modified"]);
            $ItemValue.add("Author", $SourceItem["Author"].Email);
            $ItemValue.add("Editor", $SourceItem["Editor"].Email);

            Write-Progress -Activity "Copying List Items:" -Status "Copying Item ID '$($SourceItem.Id)' from Source List ($($Counter) of $($SourceListItemsCount))" -PercentComplete (($Counter / $SourceListItemsCount) * 100)
            
            #Copy column value from Source to Destination
            $NewItem = Add-PnPListItem -List $DestinationList -Values $ItemValue -Connection $DestinationConn
  
            #Copy Attachments
            Copy-SPOAttachments -SourceItem $SourceItem -DestinationItem $NewItem
  
            Write-Host "Copied Item ID from Source to Destination List:$($SourceItem.Id) ($($Counter) of $($SourceListItemsCount))"
            $Counter++
        }
   }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message 
    }
}
  
#Set Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/Retail"
$SourceListName = "Projects"
 
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/Sales"
$DestinationListName = "Projects"
 
#Connect to Source and destination sites
$SourceConn = Connect-PnPOnline -Url $SourceSiteURL -Interactive -ReturnConnection
$SourceList = Get-PnPList -Identity $SourceListName -Connection $SourceConn
 
$DestinationConn = Connect-PnPOnline -Url $DestinationSiteURL -Interactive -ReturnConnection
$DestinationList = Get-PnPList -Identity $DestinationListName -Connection $DestinationConn
  
#Call the Function to Copy List Items between Lists
Copy-SPOListItems -SourceList $SourceList -DestinationList $DestinationList

如果您要在站点之间复制列表项,请确保首先在目标站点中创建所有父查找列表!

总之,通常需要将 SharePoint Online 中的项目从一个列表复制到另一个列表,这可以手动完成,但如果对大量项目执行此操作,可能会非常耗时且容易出错。 PowerShell 允许您自动执行重复性任务并同时对多个项目执行操作。使用 PowerShell 将项目从 SharePoint Online 中的一个列表复制到另一个列表是一种快速有效的数据传输方法。通过连接到 SharePoint Online、检索列表并使用 Add() 方法复制项目,您可以轻松地将项目从一个列表复制到另一个列表。

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

取消回复欢迎 发表评论:

关灯