Friday, 30 October 2015

Get Version Details of a Particular Item in a Sharepoint List.

# Get each version details of a particular Item in a List/Library
# To Know the value in a custom column when the particular version was created
# For e.g Lets say there is custom column named 'User' in a list. 
# And each time when the particular item is Modified by different user then the 'User' column  also get updated  per each version.
# Such as for Version 1.0=====> User: Mr. ABC
#                    Version 2.0=====> User: Mr. XYZ
#                    Version 3.0=====> User: Mr. DEF
# So this code helps to retrieve the value in 'User' Column

Add-PSSnapin Microsoft.Sharepoint.Powershell

$WebUrl = “http://server:port/Site”
$SPWeb = Get-SPWeb -Identity $WebUrl
$SPList = $SPWeb.Lists["List Name"]
$SPItem = $SPList.Items | Where-Object {$_['ID'] -eq "100"}
# Counts the number of Versions of an item
$VerCount = $SPItem.Versions.Count;
# Declare an array
$Test = @(0..$VerCount)
# Create a dictionary object
$dictionary = New-Object 'System.Collections.Generic.Dictionary[String,String]' 
# Iterate through each version of an item
foreach($Ver in $SPItem.Versions)
{
    if($VerCount -gt 0)
    {
        $VersionLabel = $Ver.VersionLabel 
        $CustomColVal = $Ver.Item("Custom Column"
        $User = $UploadUser
        $dictionary.Add($VersionLabel,$CustomColVal)
    }
}
return $dictionary

No comments:

Post a Comment