Updating SharePoint List Item Metadata Using Powershell
Introduction:
This article will be helpful to address issues where metadata fields are not properly mapped during the migration. This can lead to incomplete or incorrect metadata for your SharePoint list items. To resolve this, we can use a PowerShell script to update the metadata directly.
This article will guide you through the steps to achieve this using a CSV file that contains ItemUrls and the target metadata values.
CSV File Structure:
Your CSV file should have the following structure:
- ItemURL: The URL of the SharePoint list item.
- TargetMetadataValue: The new metadata value to be updated
PowerShell Script:
Below is a PowerShell script that reads the CSV file and updates the metadata for each item in the SharePoint list.
<#
Copyright = '(c) 2019 Proventeq Ltd. All Rights Reserved.'
Version = 1.0
#>
$csvFilePath = "D:\Proventeq.SPO.SetListItemMetadata_template.csv"
$siteUrl = "https://pvqvs.sharepoint.com/sites/Dev-Modern"
$fieldToUpdate = "Title"
Function Write-PVQLog
{
<#
.SYNOPSIS
Adds content to log file and writes console message
.DESCRIPTION
Adds content to log file and writes console message
.EXAMPLE
Write-PVQLog "Started script execution..."
#>
Param ([string]$logstring)
$logTime = Get-Date -Format "dd-MM-yyyy_hh:mm"
Write-Host "[$logTime] $logstring"
Add-content $global:logFile -value "[$logTime] $logstring"
}
#Set list item metadata
Function Set-PVQListItemMetadata
{
Param ($allItems)
#Status Collection
$statusColl = @()
foreach($item in $allItems)
{
try
{
$itemUrl = $item.ItemUrl
#$targetLinkUrl = $item.TargetLinkUrl.Replace(',', "%2C")
$targetMetadataValue = $item.TargetMetadataValue
$result = new-object PSObject
$result | add-member -membertype NoteProperty -name "Item Url" -Value $itemUrl -Force
$result | add-member -membertype NoteProperty -name "Target Link Url" -Value $targetMetadataValue -Force
Write-PVQLog "Setting metadata for list item: $itemUrl"
$ctx = Get-PnPContext
$web = Get-PnPWeb
$listItem = $web.GetListItem($itemUrl)
$ctx.Load($listItem)
Invoke-PnPQuery
$list = $listItem.ParentList
$ctx.Load($list)
Invoke-PnPQuery
$updated = Set-PnPListItem -List $list -Identity $listItem -Values @{"$fieldToUpdate"= $targetMetadataValue} -SystemUpdate -ErrorAction Stop
$result | add-member -membertype NoteProperty -name "Status" -Value "Succeed" -Force
$result | add-member -membertype NoteProperty -name "Message" -Value "Metadata updated successfully" -Force
$statusColl += $result
Write-PVQLog "Successfully updated metadata for list item: $itemUrl"
}
catch
{
$errorMessage = $_.Exception.Message
$result | add-member -membertype NoteProperty -name "Status" -Value "Failed" -Force
$result | add-member -membertype NoteProperty -name "Message" -Value $errorMessage -Force
$statusColl += $result
Write-PVQLog "Failed while setting metadata for list item: $itemUrl"
Write-PVQLog "Error: $errorMessage"
}
}
return $statusColl
}
#Start PowerShell script execution.
Function Start-PVQSetListItemMetadata
{
#Log file creation time
$logFileTime = Get-Date -Format "ddMMyyyy_hhmm"
#Output Log file path, create if not exists
$outputLogFilePath = $env:USERPROFILE + "\Proventeq\Logs\SetListItemMetadata"
If(!(test-path $outputLogFilePath))
{
New-Item -ItemType Directory -Force -Path $outputLogFilePath
}
#Log file name with started time.
$global:logFile = $outputLogFilePath + "\log_" + $logFileTime +".log"
$cred = Get-Credential
Write-PVQLog "Importing csv file"
$allCsvItems = Import-Csv -Path $csvFilePath
Write-PVQLog "Successfully imported csv file"
if($allCsvItems)
{
Write-PVQLog "Connecting to site: $siteUrl"
Connect-PnPOnline $siteUrl -UseWebLogin #-Credentials $cred
Write-PVQLog "Successfully connected to site: $siteUrl"
$results = Set-PVQListItemMetadata -allItems $allCsvItems
if($results)
{
$results | Export-csv -Path (Join-Path $outputLogFilePath "SetListItemMetadata_$logFileTime.csv") -NoTypeInformation
Write-PVQLog "Exported results file to $outputLogFilePath"
}
}
else
{
Write-PVQLog "The input csv file is either blank or invalid file path provided."
}
Write-PVQLog "Script execution completed..!"
}
#Start the script execution.
Start-PVQSetListItemMetadata
Comments