Skip to content

File copyto method does not allow new filename anymore (copyto method changed from earlier versions) (Sharepoint) #787

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

Closed
BodoBurger opened this issue Nov 21, 2023 · 1 comment
Labels

Comments

@BodoBurger
Copy link

Python: 3.11
Office365-REST-Python-Client Version: 2.5.2 vs 2.3.14


In earlier versions (e.g. 2.3.14), the copyto method of a File instance expected a target url including a filename which made it possible to copy file into a folder whithout overwriting another file with the same filename.
Now (version 2.5.2), the method only expects a folder url, and there is (apparently) no possibilty to pass a new filename (at least I dit not find any other way than to rename the file after copying; however, this does not work if there is already a file with the same original filename).

So my main question is: is this change intended?
And if it is, is there a workaround without overwriting other files?

I could not pinpoint which version changed the behaviour of the copyto method, but it has to be somewhere between 2.3.14 and 2.5.2.

Code version 2.5.2

source_path = "Shared Documents/folder/test.xlsx"
destination_path = "Shared Documents/folder/subfolder"
new_filename = "test_copy.xlsx"

file = ctx.web.get_file_by_server_relative_url(source_path).get().execute_query()
file_copy = file.copyto(destination_path, overwrite=True).executy_query()
file_copy.rename(new_filename).execute_query()

Code version 2.3.14

source_path = "Shared Documents/folder"
filename = "test.xlsx"
destination_path = "Shared Documents/folder/subfolder"
new_filename = "test_copy.xlsx"

folder = ctx.web.get_folder_by_server_relative_url(path).get().execute_query()
files = folder.expand(["Files"]).get().execute_query().files
for file in files:
    if file.name == filename:
        break

file.copyto(f"{destination_path}/{new_filename}", False).execute_query()

In the new version it is much easier to retrieve the File instance with a working copyto method, however I think the way the copyto method worked in the old version is much more useful.

@vgrem
Copy link
Owner

vgrem commented Dec 6, 2023

Greetings,

i confirm the change was intended although it indeed broke backward compatibility, apology about that. The intention was to more easily address a file and folder and pass it as a parameter into a specified method.

In terms of filename parameter, a new 2.5.3 version introduces a support for it in File.copyto and File.copyto_using_path methods as demonstrated below:

file_from = ctx.web.get_file_by_server_relative_url(
    "Shared Documents/Financial Sample.xlsx"
)
folder_to_url = "Shared Documents/archive"
new_filename = "Financial 2023.xlsx"
file_to = file_from.copyto(folder_to_url, True, new_filename).execute_query()

@vgrem vgrem closed this as completed Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants