Wednesday, November 30, 2016

SharePoint Online: Update Created By / Modified By, Created At / Modified At Field values using PowerShell CSOM

At times, you may have to create list items or set existing item's metadata fields such as Created by, Modified by, Created at, Modified values to a specific user - time stamp. Say, You are importing or migrating data from a network file share to SharePoint online and you want to keep these metadata as same as the source content.

Because, SharePoint doesn't let you to set these values from web user interface, we've to use PowerShell. Here is my PowerShell script to change the details of who created an item, modified it and when they created and modified it.

This script sets system fields (created, created by, modified, modified by) for a specific item in the list/library. Set the values in variables section appropriately and run the script.

PowerShell script to update Created by, Modified By, Created at, Modified at field values:
This PowerShell script sets item's metadata field values in SharePoint online

#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"
  
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales/"
$ListName= "Projects"
$ID=6
$UserID="Salaudeen@crescent.com"
$TimeStamp = "2015/12/01 02:10:00 AM"

#Get Credentials to connect
$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)
$ListItem = $List.GetItemById($ID) 
$Ctx.Load($ListItem)

#Update Created by & Modified By
$ListItem["Author"] = $User
$ListItem['Editor'] = $User

#Set Created on & Modified on Time values
$ListItem["Created"] =  $TimeStamp
$ListItem["Modified"] = $TimeStamp

#Update List item
$ListItem.Update()
$ctx.ExecuteQuery()

Write-host "Metadata values updated Successfully!" -f Green
and the result goes here:
sharepoint online powershell to update created by modified by created at modified at values
For Server side PowerShell script, refer: Update "Created By", "Last Modified" Metadata Fields of a List Item using PowerShell



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...