I have a document library in a SharePoint Online site, which holds many PDF files, each with several versions in the history. Is there a way I can easily download all versions of every file?
The solution I eventually found combined the SharePoint REST API, web services (as suggested by this answer) and a cunning Python script. It worked a treat.
I requested a list of files in the library using the REST API endpoint
https://example.sharepoint.com/_api/web/getFolderByServerRelativeUrl('/Test Library')/Files, then looped through each result, calling the URL from the property
uri. I was then able to loop through the returned file version list, calling the URL from the
Url property, which in turn downloaded one version of one file for each iteration of the nested loop. At this point the file can be saved with the version number appended to the file name.
My actual script was a fair bit more complicated than this, since I also wanted to retrieve and save the value from another field, Revision. This required making two SOAP requests per file to
https://example.sharepoint.com/_vti_bin/Lists.asmx*; the first for the Version field and the second for the Revision field (since the web services medthod
GetVersionCollection can only return one field at a time). I then filtered out the latest version of each file (I already had a copy of these) and all but the latest version for each value of Revision, and saved the files with the Revision value appended instead of the version.
* If you visit this URL in a browser you'll be presented with documentation for each method, with useful examples demonstrating the rather complicated XML required.
We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.