Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Sharepoint - "404 Client Error: Not Found for url" When try download file with # in name

Open JLTs66 opened this issue 3 years ago • 2 comments

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)

JLTs66 avatar May 08 '22 01:05 JLTs66

Same problem when % in name (same that #)

JLTs66 avatar May 10 '22 17:05 JLTs66

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...

JLTs66 avatar May 10 '22 20:05 JLTs66

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

vgrem avatar Oct 18 '22 20:10 vgrem