Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to download old version files? #400

Closed
kazukiaaaa opened this issue Aug 25, 2021 · 3 comments
Closed

How to download old version files? #400

kazukiaaaa opened this issue Aug 25, 2021 · 3 comments

Comments

@kazukiaaaa
Copy link

kazukiaaaa commented Aug 25, 2021

When downloading minor version 0.5 I understand that you can get the file by the following URL.
URL:https://tenant.sharepoint.com/sites/base/_api/web/GetFileByServerRelativeUrl('/sites/base/sample.txt')/versions(5)/$value

Is it possible to download a file from the above URL using this function?
function: ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()

@vgrem vgrem added the question label Aug 25, 2021
@stefanstapinski
Copy link

Where do you get "/versions(5)/$value" from?

I'm still having trouble with all examples, I can read the library site but I'm not able to read items in a folder

@vgrem
Copy link
Owner

vgrem commented Sep 6, 2021

Greetings!

Managing file versions were somewhat indeed limited and the only option to download the content for a specific version was to construct a custom request.

Starting from 2.3.8 version the library natively supports managing file versions, here are a few examples:

Example: download a specific file version content

ctx = ClientContext(test_team_site_url).with_credentials(credentials)
file_url = "/sites/team/Shared Documents/Guide.docx"
version = ctx.web.get_file_by_server_relative_path(file_url).versions.get_by_id(512)  #address file version by version identifier
download_path = os.path.join(tempfile.mkdtemp(),  os.path.basename(file_url))
with open(download_path, "wb") as local_file:
    file = version.download(local_file).execute_query()

Example: delete a file version

ctx = ClientContext(test_team_site_url).with_credentials(credentials)
file_url = "/sites/team/Shared Documents/Guide.docx"
target_file = ctx.web.get_file_by_server_relative_path(file_url)
versions = target_file.versions.top(1).get().execute_query()
versions[0].delete_object().execute_query()

All another API methods are also supported.

@vgrem vgrem closed this as completed Sep 6, 2021
@kazukiaaaa
Copy link
Author

Thank you for your generous support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants