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()
0

There are 0 best solutions below