SharePoint Online: copie valores de una columna a otra usando PowerShell
Requisito: Copie valores de una columna a otra columna en una lista de SharePoint Online.
PowerShell para copiar el valor de la columna de la lista en SharePoint Online
Para copiar valores entre columnas en la misma lista, use este script 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]}
}
Para tipos de campos como líneas individuales de texto o elección, este script funciona bien. Sin embargo, tenemos que trabajar un poco más para otro tipo de campo, como hipervínculo, persona o grupo, etc.
SharePoint Online: copiar valores de un campo a otro usando PowerShell
A continuación se explica cómo copiar datos de una columna a otra. Asegúrese de que la nueva columna sea del mismo tipo.
#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