
Updating SharePoint list item metadata manually can be a time-consuming process, especially when dealing with a large number of items, specially in case of content migration. This script automates the process, saving time and reducing the risk of errors.
In this blog post, we’ll explore a PowerShell script authored by Adnan Amin that updates the metadata of a SharePoint list item. Specifically, it modifies the “Created”, “CreatedBy”, “Modified”, and “ModifiedBy” fields.
Key Scenarios why we need to update list item metadata values
- Data Migration: During data migration, the metadata of SharePoint list items may need to be updated to match the new data source. This script can automate this process.
- Correcting Inaccurate Metadata: If the metadata of SharePoint list items is found to be inaccurate, this script can be used to correct it.
- Bulk Updates: If you need to update the metadata of multiple SharePoint list items, doing so manually would be time-consuming and error-prone. This script allows you to perform these updates quickly and accurately.
PowerShell script for list item Metadata value updates
Here’s the script we’ll be discussing:
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#this function will require site url, list name, list item id which need to be updated and timestamp & user details for created and modified fields
function UpdateItemMetadata($siteurl, $listname, $itemid, $createdbyUser, $modifedByUser, $createdDate, $modifiedDate)
{
try{
$cred = Get-Credential
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.Username, $cred.Password)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = $credentials
$eCreatedByUser = $ctx.Web.EnsureUser($createdbyUser)
$ctx.Load($eCreatedByUser)
$eModifedByUser = $ctx.Web.EnsureUser($modifedByUser)
$ctx.Load($eModifedByUser)
$list=$ctx.Web.Lists.GetByTitle($listname)
$item = $list.GetItemById($itemid)
$ctx.Load($item)
#Below Powershell will update the user details for created and modified columns
$item["Author"] = $eCreatedByUser
$item['Editor'] = $eModifedByUser
#Below Powershell will update the user details for created and modified columns
$item["Created"] = $createdDate
$item["Modified"] = $modifiedDate
$item.Update()
$ctx.ExecuteQuery()
Write-host "All fields updated Successfully!" -f Green
}
catch{
Write-Host $_.Exception.Message -ForegroundColor Red
}
}
This script starts by loading the SharePoint Client assemblies which are required to interact with SharePoint Online. It then defines a function UpdateItemMetadata that takes several parameters including the SharePoint site URL, list name, item ID, user details for the “CreatedBy” and “ModifiedBy” fields, and timestamps for the “Created” and “Modified” fields.
Inside the function, it first prompts for credentials using Get-Credential and creates a SharePoint Online credentials object. It then creates a ClientContext object for the SharePoint site. The EnsureUser method is used to get the user objects for the “CreatedBy” and “ModifiedBy” fields. This method checks if a user exists within the site collection.
It fetches the list and the specific item in the list using the provided list name and item ID. The “Author” and “Editor” fields of the item (which correspond to “CreatedBy” and “ModifiedBy”) are updated with the user objects. The “Created” and “Modified” fields of the item are updated with the provided timestamps.
The item is updated using the Update method and the changes are submitted to the server using ExecuteQuery. If the update is successful, a success message is printed. If an error occurs, the error message is printed.
Function Call
Finally, the function is called with specific parameters to update a particular item in a SharePoint list.
UpdateItemMetadata -siteurl "https://mstalk.sharepoint.com" -listname "Documents" -itemid "4" -createdbyUser "demo@mstechtalk.com" -modifedByUser "spadmin@mstechtalk.com" -createdDate "2019/15/04 12:15:00 AM" -modifiedDate "2019/15/6 04:30:00 PM"
By using this script, administrators can ensure that all specified fields are updated with the correct settings. This is particularly useful in large organizations where there may be a need to update multiple fields across various lists.






No Comments