kwizcom banner advertisement

Compare SharePoint List Item Version History using PowerShell

Requirement: Compare version history data of SharePoint List items and update a metadata column based on a particular field's value change.

Bit background: We've a project tracking list with "Status" column. We wanted to have a insight on when a particular item's status was set to "Rejected".

PowerShell to Compare Version History Data and update a Column value:
As far solution to the above requirement, lets compare list item versions to capture when a particular item's "status" column was changed to "rejected", and get the created date of the specific version to update "Rejected Date" column of the list item. Lets compare version history in SharePoint using PowerShell:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Parameters
$SiteURL="http://intranet.crescent.com"
$ListName = "Projects"

#Get web and List 
$Web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

#Get all list items
$ListItems = $List.Items

#Iterate through each list item
Foreach($Item in $ListItems)
{
    #Iterate through each version
    ForEach($Version in $Item.Versions) 
    {
        #Check if the status column is "Rejected"
        If($($version['Status']) -eq "Rejected")
        {
            #Update Rejected Date value of the item from version
            $Item["RejectedDate"] = $($version.Created)
            $Item.SystemUpdate()
            Write-host "ID:$($item.id), Version:$($version.VersionLabel), CreatedBy:-$($version.CreatedBy.User.DisplayName), Status:-$($version['Status'])"
            Break
        }
    }
}
This script compares each version of the list item and updates "RejectedDate" column value.
Compare SharePoint List Item Version History using PowerShell Compare SharePoint List Item Version History using PowerShell Reviewed by Salaudeen Rajack on 5:04 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.