0

I've used PowerShell code similar to this Reference which gets a SP list and iterates thru the items (note that I use PnP). This works fine except that the loop that gets versions of list items doesn't execute at all b/c the Versions member doesn't seem to exist. I'm sure there is version info. I'm using SP 2013. Any ideas appreciated!

Connect-PnPOnline –Url "http://MyServer/MySite" –Credentials (Get-Credential)
$ItemsColl = (Get-PnPListItem -List "MySPList")

#Loop through each item
foreach ($item in $ItemsColl)
{
    write-host $($item["MyFieldName"])      # WORKS FINE!

    #Iterate each version   
    foreach($version in $item.Versions) # FAILS! THIS OBJECT IS ALWAYS NULL (ALTHOUGH VERSIONS DO EXIST ON THIS SITE/LIST) SO LOOP DOESN'T EXECUTE.
    { 
        write-host $($version.VersionLabel)
    }
}
0

Apparently, version information needs to be loaded separately, as part of the file property. Try something like this:

Connect-PnPOnline –Url "http://MyServer/MySite" –Credentials (Get-Credential)
$ItemsColl = (Get-PnPListItem -List "MySPList")
$ctx = Get-PnPContext

#Loop through each item
foreach ($item in $ItemsColl)
{
  $file = $item.file
  $fileversions = $file.Versions
  $ctx.load($file)
  $ctx.load($fileversions)
  $ctx.ExecuteQuery()
  Write-Host $file.Name,$fileversions.VersionLabel        
}

Courtesy: https://techcommunity.microsoft.com/t5/SharePoint-Developer/How-Do-I-get-the-version-of-a-file-using-PnP-Powershell/m-p/156100

  • Thanks for your reply. The technique you mention (executing the Context) may be necessary. However, I think I'm going after the Versions property of the ListItem object, not the File property. My goal is to output the data for all the versions of each item. Interestingly, when I list the properties of the ListItem object, I don't see a Versions property at all (I do see a File property, btw). Unclear why - sample code I've seen posted around uses this property - even when the sample code uses the Context, as in your example, it still needs to access Versions, which isn't there for me. – wme Oct 16 '19 at 15:23
  • Looks like this isn't doable with Get-PnPListItem (elsewhere I've read that the Versions object isn't returned). I'm trying an alternative: accessing the list thru web services. – wme Oct 23 '19 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.