In this article, we are going to learn "How to update list metadata using PowerShell".
In some cases we want a particular person to be the created by and modified by and we also want to update the time stamp of modified at and created. We can achieve this using PowerShell.
Power shell to update Created By,Modified By, Created,Modified field values
--Load SharePoint CSOM Assemblies--
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"
--Replace the tenantName and listName with your site name and list name--
$SiteUrl = "https://tenantName.sharepoint.com/sites/training/"
$ListName= "listName"
$ID=1
----Replace the Username and Password with your one--
$Username = "userId"
$Password = "pwd" | ConvertTo-SecureString -AsPlainText -Force
#Get Credentials to connect
$Cred = New-Object pscredential -ArgumentList ($Username , $Password)
#$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $Credentials
#Get the User
$User = $Ctx.Web.EnsureUser($UserID)
$ctx.Load($user)
#Get the list Item
$List = $ctx.Web.Lists.GetByTitle($ListName)
$Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$Item = $List.GetItemById($ID);
$ctx.Load($List)
$ctx.Load($Item)
$ctx.ExecuteQuery()
--Replace the UserID with the your user Id which you want to be as Modified and Created by--
$UserID="[email protected]"
$TimeStamp = "2019/03/16"
$Item['Editor'] = $User
$Item['Author'] = $User
$Item["Created"] = $TimeStamp
$Item.Update()
$ctx.ExecuteQuery()
Thanks!!!!
Comments
Post a Comment