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

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

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

SharePoint Online:使用 PowerShell 将值从一列复制到另一列


要求:将 SharePoint Online 列表中的一列中的值复制到另一列。

PowerShell 在 SharePoint Online 中复制列表列值

要在同一列表中的列之间复制值,请使用以下 PnP PowerShell 脚本:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$SourceColumn = "Project_x0020_Name" #Internal Name of the Fields
$DestinationColumn = "ProjectName"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get all items from List
$ListItems = Get-PnPListItem -List $Listname

#Copy Values from one column to another
ForEach ($Item in $ListItems) 
{
    Set-PnPListItem -List $Listname -Identity $Item.Id -Values @{$DestinationColumn = $Item[$SourceColumn]}
}

对于单行文本或选择之类的字段类型,此脚本可以正常工作。然而,我们必须为其他字段类型做更多的工作,例如超链接、个人或组等。

SharePoint Online:使用 PowerShell 将值从一个字段复制到另一个字段

以下是如何将数据从一列复制到另一列。确保新列的类型相同。


#Function to copy values from one column to another in a list
Function Copy-SPOListColumnValues()
{
    Param
    (
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $SourceColumnName,
        [Parameter(Mandatory=$true)] [string] $TargetColumnName
    )
    Try {
        #Get All Items from the Source List in batches 
        Write-Progress -Activity "Reading Source..." -Status "Getting Items from Source List. Please wait..."
        $ListItems = Get-PnPListItem -List $ListName -PageSize 2000
         Write-host "Total Number of Items Found:"$ListItems.count
 
        #Get fields to Update from the Source List - Skip Read only, hidden fields, content type and attachments
        $SourceField = Get-PnPField -List $ListName -Identity $SourceColumnName
        $TargetField = Get-PnPField -List $ListName -Identity $TargetColumnName

        #Loop through each item in the source and Get column values, add them to target column
        [int]$Counter = 1
        ForEach($ListItem in $ListItems)
        {  
            $ItemValue = @{}
            #Check if the Field value is not Null
            If($ListItem[$SourceField.InternalName] -ne $Null)
            {
                #Handle Special Fields
                $FieldType  = $SourceField.TypeAsString
 
                If($FieldType -eq "User" -or $FieldType -eq "UserMulti" -or $FieldType -eq "Lookup" -or $FieldType -eq "LookupMulti") #People Picker or Lookup Field
                {
                    $LookupIDs = $ListItem[$SourceField.InternalName] | ForEach-Object { $_.LookupID.ToString()}
                    $ItemValue.add($TargetField.InternalName,$LookupIDs)
                }
                ElseIf($FieldType -eq "URL") #Hyperlink
                {
                    $URL = $ListItem[$SourceField.InternalName].URL
                    $Description  = $ListItem[$SourceField.InternalName].Description
                    $ItemValue.add($TargetField.InternalName,"$URL, $Description")
                }
                ElseIf($FieldType -eq "TaxonomyFieldType" -or $FieldType -eq "TaxonomyFieldTypeMulti") #MMS
                {
                    $TermGUIDs = $ListItem[$SourceField.InternalName] | ForEach-Object { $_.TermGuid.ToString()}                    
                    $ItemValue.add($TargetField.InternalName,$TermGUIDs)
                }
                Else
                {
                    #Get Source Field Value and add to Hashtable
                    $ItemValue.add($TargetField.InternalName,$ListItem[$SourceField.InternalName])
                }
                Write-Progress -Activity "Copying List Items:" -Status "Copying Item ID '$($ListItem.Id)' from Source List ($($Counter) of $($ListItems.count))" -PercentComplete (($Counter / $ListItems.count) * 100)

                #Copy column value from source to target
                Set-PnPListItem -List $ListName -Identity $ListItem -Values $ItemValue | Out-Null
 
                Write-Host "Copied Values from Source to Target Column of Item '$($ListItem.Id)' ($($Counter) of $($ListItems.count)) "
                $Counter++
            }
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message 
    }
}

#Set Parameter values 
$SiteURL  = "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "ProjectTracking"
#Source and Target column Internal Names
$SourceColumnName = "ProjectTeam"
$TargetColumnName = "TeamMembers"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  #-Interactive

#Call the Function to copy sharepoint list column values from one to another 
Copy-SPOListColumnValues -ListName $ListName -SourceColumnName $SourceColumnName -TargetColumnName $TargetColumnName

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

取消回复欢迎 发表评论:

关灯