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

Move Folder to another Folder

Open Thulasi711 opened this issue 3 years ago • 1 comments

source_url="/sites/Testing907/Shared Documents/rest" destination_url="/sites/Testing907/Shared Documents/Renamed"

ctx.web.ensure_folder_path(destination_url).execute_query()

folder = ctx.web.get_folder_by_server_relative_url(source_url) folder.move_to(destination_url , 1) ctx.execute_query()

using the same as to move the folder, but still getting below error. Please help on this

File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\site-packages\office365\runtime\client_runtime_context.py", line 139, in execute_query self.pending_request().execute_query() File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\site-packages\office365\runtime\client_request.py", line 84, in execute_query raise ClientRequestException(*e.args, response=e.response) office365.runtime.client_request_exception.ClientRequestException: (None, None, '429 Client Error: for url: https://xxx.sharepoint.com/sites/Testing907/_api/Web')

Process finished with exit code 1

Thulasi711 avatar Jun 30 '22 14:06 Thulasi711

source_url="/sites/Testing907/Shared Documents/rest" destination_url="/sites/Testing907/Shared Documents/Renamed"

ctx.web.ensure_folder_path(destination_url).execute_query()

folder = ctx.web.get_folder_by_server_relative_url(source_url) folder.move_to_by_path(destination_url , 1) ctx.execute_query()

Even moving to path also same issue. please help its very urgent.

C:\Users\AppData\Local\Programs\Python\Python39\python.exe "C:/Users//Desktop/SP/Share_Point/copy folder.py"

Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\office365\runtime\client_request.py", line 80, in execute_query response.raise_for_status() File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 960, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://xxx.sharepoint.com/sites/Testing907/_api/Web/getFolderByServerRelativePath(/sites/Testing907/Shared Documents/Renamed)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\Desktop\SP\Share_Point\copy folder.py", line 65, in move_to_by_path(source_file,destination_url,False).get().execute_query() File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\office365\runtime\client_object.py", line 41, in execute_query self.context.execute_query() File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\office365\runtime\client_runtime_context.py", line 139, in execute_query self.pending_request().execute_query() File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\office365\runtime\client_request.py", line 84, in execute_query raise ClientRequestException(*e.args, response=e.response) office365.runtime.client_request_exception.ClientRequestException: ('-1, Microsoft.SharePoint.Client.InvalidClientQueryException', 'The expression "Web/getFolderByServerRelativePath(/sites/Testing907/Shared Documents/Renamed)" is not valid.', '400 Client Error: Bad Request for url: https://xxx.sharepoint.com/sites/Testing907/_api/Web/getFolderByServerRelativePath(/sites/Testing907/Shared Documents/Renamed)')

Process finished with exit code 1

Thulasi711 avatar Jul 05 '22 08:07 Thulasi711

Hi,

it appears Folder.move_to method was indeed generating invalid request, thank you for reporting it. It has been resolved in 2.3.14 release, here is an example on how to move a folder:

ctx = ClientContext(site_url).with_credentials(credentials)

source_folder_url = "/sites/team/Shared Documents/2021"
target_folder_url = "/sites/team/Shared Documents/2022"
source_folder = ctx.web.get_folder_by_server_relative_url(source_folder_url)
source_folder.move_to(target_folder_url).execute_query()

vgrem avatar Sep 24 '22 12:09 vgrem