0

I am trying to upload a file into Sharepoint but the code fails with the below error code.

office365.runtime.client_request_exception.ClientRequestException: ('-2147024809, System.ArgumentException', 'Server relative urls must start with SPWeb.ServerRelativeUrl', "400 Client Error: Bad Request for url: https://<company_name>.sharepoint.com/sites/<site_folder>/_api/Web/getFolderByServerRelativeUrl('Shared%20Documents%2FGeneral%2F3.%20PAYMENTS%20Operations%2FTIES%2FCRS%20Report%20files')/Files/add(overwrite=true,url='%2Ftmp%2Ftmp1lsc2asr%2FXML_REPORT_df185e51-2df0-48be-ad7f-cf8a3ae31914.xml')")

I am trying to upload a file in sharepoint with the below folder structure.

Shared Documents > General > 3. PYAMENTS Operations > TIES > CRS Report files

I have written the below code snippet to upload a file into sharepoint. Please let me know where I am going wrong.

    full_url = f'https://<company_name>.sharepoint.com/sites/<site_folder>/'
    relative_url = f'Shared Documents/General/3.PAYMENTS Operations/TIES/CRS Report files'
    ctx = get_sharepoint_context_using_user()
    
    print(ctx)
    
    target_folder = ctx.web.get_folder_by_server_relative_url(relative_url)
    
    print(target_folder)

    # Open file in binary mode
    with open(temp_file, 'rb') as content_file:
        file_content = content_file.read()
        #print(file_content)
        target_folder.upload_file(temp_file, file_content).execute_query()
Rupesh
  • 5
  • 2
  • can you please check the below answer? https://sharepoint.stackexchange.com/a/229886/78095 – jimas13 Jan 09 '23 at 19:45
  • My relative URL doesnt have the "/" at the begining. But still it is not working. Any other suggestions please. – Rupesh Jan 10 '23 at 06:36
  • the answer states that you should use "/sites//Shared Documents/General/3.PAYMENTS Operations/TIES/CRS Report files" – jimas13 Jan 10 '23 at 08:22
  • I have tried both the things but it didnt work. Getting the same error message – Rupesh Jan 10 '23 at 09:53

0 Answers0