Sharepoint - "404 Client Error: Not Found for url" When try download file with # in name
Sharepoint Office 365 (online). Try to use method download_session, work normally except when "#" caract in the name. The code raise :
office365.runtime.client_request_exception.ClientRequestException: ('-2130575338, Microsoft.SharePoint.SPException', "Le fichier /personal/user_test_ca/Documents/File with#.doc n'existe pas.", "404 Client Error: Not Found for url: https://mycon.sharepoint.com/personal/user_test_ca/_api/Web/getFileByServerRelativeUrl('%2Fpersonal%2Fuser_test_ca%2FDocuments%2FFile with%23.doc')?$select=ServerRelativeUrl")
To repeat the problem, just put file with a .doc with a hash (#) in the name, and download it with the method "download_session".
Exemple code :
source_file = ctxSrc.web.get_file_by_server_relative_url(props["ServerRelativeUrl"])
with open(os.path.join(temp_dir,name), "wb") as local_file:
source_file.download_session(local_file, print_download_progress).execute_query()
Note : I try with replacement of "get_file_by_server_relative_url" by "get_file_by_id" (with UniqueID), same issue.
End of the error tracestack :
File "copyStoS.py", line 167, in copyFolder
source_file.download_session(local_file, print_download_progress).execute_query()
File "...\lib\site-packages\office365\runtime\client_object.py", line 41, in execute_query
self.context.execute_query()
File "...\lib\site-packages\office365\runtime\client_runtime_context.py", line 134, in execute_query
self.pending_request().execute_query()
File "...\lib\site-packages\office365\runtime\client_request.py", line 84, in execute_query
raise ClientRequestException(*e.args, response=e.response)
Same problem when % in name (same that #)
I discover that the module will always use "GetFileByServerRelativeUrl" in url call. Even if we use "get_file_by_id" just before.
Maybe it's possible to force the module to not use GetFileByServerRelativeUrl when we download/moveto/etc...
Hi there,
the root cause why this error occurs in the first place is related with internals of server side getFileByServerRelativeUrl method implementation and summarized in Supporting % and # in files and folders with the ResourcePath API:
Support for
%and#in files and folders is being deployed within SharePoint Online. Unfortunately, due to existing API structures and calling patterns, working with these file names can sometimes become ambiguous.
Resolution
Prefer Web.get_file_by_server_relative_path over Web.get_file_by_server_relative_url method which supports those symbols in file name. As of File.download_session method, starting from 2.3.13 version it uses path based addressing as well by default.
Related issues: #486