SOLVED

Using SharePoint PNP to get item properties on older version

%3CLINGO-SUB%20id%3D%22lingo-sub-915726%22%20slang%3D%22en-US%22%3EUsing%20SharePoint%20PNP%20to%20get%20item%20properties%20on%20older%20version%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-915726%22%20slang%3D%22en-US%22%3E%3CP%3ESo%20I%20made%20a%20mistake%20and%20overwrote%20one%20column%20of%20metadata%20(Donor_Name)%20in%20a%20SharePoint%20library%20using%20a%203rd%20party%20tool%20(my%20mistake%20not%20the%20tool).%20I%20can%20see%20that%20the%20older%20versions%20of%20the%20documents%20still%20have%20the%20correct%20metadata%20so%20I%20would%20like%20to%20grab%20the%20metadata%20from%20the%20first%20versions%20of%20these%20documents%20which%20I%20can%20then%20use%20to%20fix%20the%20documents.%20I%20have%20the%20code%20below%20which%20lets%20me%20retrieve%20the%20versions.%20What%20I%20want%20is%20something%20like%20%24file%5B0%5D%20that%20lets%20me%20retrieve%20the%20file%20name%20and%20Donor_Name%20and%20export%20it%20to%20a%20CSV%3F%3C%2FP%3E%3CP%3EIs%20this%20possible%3F%20Otherwise%20I%20will%20need%20to%20find%20some%20other%20way%20to%20retrieve%20the%20data%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%24site%20%3D%20'%3CMY%20site%3D%22%22%3E'%0A%24Library%20%3D%20'%3CLIBRAY%3E'%0A%0AConnect-PnPOnline%20-Url%20%24site%20-Credentials%20%3CSAVE%20creds%3D%22%22%3E%0A%24grants%20%3D%20get-pnplist%20-Identity%20%24Library%0A%24ctx%20%3D%20Get-PnPContext%0A%24documents%20%3DGet-PnPListItem%20-List%20%24grants%0A%0A%24versions%20%0Aforeach%20(%24item%20in%20%24documents)%0A%7B%0A%20%20%20%20%20%20%20%20%24file%20%3D%20%24item.file%0A%20%20%20%20%20%20%20%20%24fileversions%20%3D%20%24file.Versions%0A%20%20%20%20%20%20%20%20%24ctx.load(%24file)%0A%20%20%20%20%20%20%20%20%24ctx.load(%24fileversions)%0A%20%20%20%20%20%20%20%20%24ctx.ExecuteQuery()%0A%20%20%20%20%20%20%20%20%0A%7D%3C%2FSAVE%3E%3C%2FLIBRAY%3E%3C%2FMY%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BAny%20ideas%20are%20welcome%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-915726%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDocument%20Library%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

So I made a mistake and overwrote one column of metadata (Donor_Name) in a SharePoint library using a 3rd party tool (my mistake not the tool). I can see that the older versions of the documents still have the correct metadata so I would like to grab the metadata from the first versions of these documents which I can then use to fix the documents. I have the code below which lets me retrieve the versions. What I want is something like $file[0] that lets me retrieve the file name and Donor_Name and export it to a CSV?

Is this possible? Otherwise I will need to find some other way to retrieve the data?

 

 

$site = '<My Site>'
$Library = '<Libray>'

Connect-PnPOnline -Url $site -Credentials <save creds>
$grants = get-pnplist -Identity $Library
$ctx = Get-PnPContext
$documents =Get-PnPListItem -List $grants

$versions 
foreach ($item in $documents)
{
        $file = $item.file
        $fileversions = $file.Versions
        $ctx.load($file)
        $ctx.load($fileversions)
        $ctx.ExecuteQuery()
        
}

 

 

 Any ideas are welcome? 

1 Reply
Highlighted
Best Response confirmed by Paul Rudy (Occasional Contributor)
Solution

Actually, I found another way to get what I needed without using SharePoint. I was able to use the information at https://sharepoint.stackexchange.com/questions/190021/exporting-item-list-version-history to export the version history of the all of the files. I was then able to use Excel to end up with a list that had the metadata before the change and, finally, I used Sharegate to fix the metadata tags.