Feb 12 2018 01:27 PM
Hi
I am trying to get the version of a file in a document library using PnP Powershell.
Connect-PnPOnline -Url <url>
$ListItems = Get-PnPListItem -List <MyDocLibrary>
for each ($item in $ListItems)
{
$file = $item.file
$fileversions = $file.Versions
Write-Host $file, $fileversions
}
But this does not seem to work.
Anybody any ideas ?
Thanks
Nigel
Feb 13 2018 03:15 AM
You can try this script
Connect-PnPOnline -Url <url> $ListItems = Get-PnPListItem -List <MyDocLibrary> $ctx= Get-PnPContex foreach ($item in $ListItems) { $file = $item.file $fileversions = $file.Versions $ctx.load($file) $ctx.load($fileversions) $ctx.ExecuteQuery() Write-Host $file.Name,$fileversions.VersionLabel }
Feb 14 2018 07:07 AM
Works great (as long as you put the t on the end of Get-PnPContex !)
What about if I only want the latest version ?
Regards
Nigel
Feb 14 2018 09:46 AM
@Nigel_Price9911 something like the following should help you get started, I have not tested this.
for (int i = versions.Count; i > 0; i--)
{
FileVersion version = file.Versions[i - 1];
string label = version.VersionLabel;
string id = version.ID.ToString();
string filename = Path.GetFileName(version.Url);
string ext = Path.GetExtension(version.Url);
string tmp1 = version.IsCurrentVersion;
string tmp2 = version.Url;
}
Feb 16 2018 04:22 AM - edited Feb 21 2018 09:06 PM
Feb 16 2018 04:22 AM - edited Feb 21 2018 09:06 PM
You can try this modified script
Connect-PnPOnline -Url <url> $ListItems = Get-PnPListItem -List <MyDocLibrary> $ctx= Get-PnPContext foreach ($item in $ListItems) { $file = $item.file $ctx.load($file) $ctx.ExecuteQuery() Write-Host $file.Name,$file.UIVersionLabel }
Sep 23 2020 04:04 AM
@Nigel_Price9911 For SharePoint Online I'm using following code:
Get-PnPFile -Url $item.ServerRelativeUrl -Path $destinationFolderPath -AsFile -Force # Latest version
$ctx= Get-PnPContext
$ctx.Load($item.Versions)
$ctx.ExecuteQuery()
foreach ($version in $item.Versions)
{
$versionValue = $version.VersionLabel
$str = $version.OpenBinaryStream()
$ctx.ExecuteQuery()
$filename = (Split-Path $item.ServerRelativeUrl -Leaf) + "." + $versionValue
$filepath = Join-Path $destinationFolderPath $filename
$fs = New-Object IO.FileStream $filepath ,'Append','Write','Read'
$str.Value.CopyTo($fs) # Older version
$fs.Close()
}
Oct 16 2020 09:04 AM
@AlyaKoni Works like a charm, thank you!
Jun 05 2023 05:43 PM
@AlyaKoni How could we also retrieve the CheckIn Comment for each version?
Jun 05 2023 11:10 PM