'Python SharePoint API office365-rest-api fails when trying to update sharepoint columns
I am uploading a file to SharePoint successfully using the office365-rest-api, but when I come to set the metadata in the SharePoint columns I'm getting an error. This only happens when I have a file with a very long filename (around 180 characters). Files with short filenames work fine.
My code is
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file_system_object_type import FileSystemObjectType
from office365.sharepoint.files.file_creation_information import FileCreationInformation
targetFolderUrl = ctx.web.ensure_folder_path(SP_Location).execute_query()
targetFile = targetFolderUrl.upload_file(fileName, fileContent)
ctx.execute_query()
listItem=targetFile.listItemAllFields
listItem.set_property('COMMITTEE','SM_Team')
listItem.update()
ctx.execute_query(
The error I'm getting is :-
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://verylongurl+filename/listItemAllFields
Is there a limit on the number of characters the url +filename is? If so, how come it uploads the file ok, but only fails when I set the columns?
Solution 1:[1]
The maximum length of the Request URI for requests to our REST API endpoints is 4096 characters. If the request URI exceeds this length, you should see the following error message in the response:
URI length exceeds the configured limit of 4096 characters
You can refer to following code to upload a file to sharepoint
import os
from office365.sharepoint.client_context import ClientContext
from tests import test_user_credentials, test_team_site_url
test_team_site_url = test_team_site_url
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
path = "../../data/report #123.csv"
with open(path, 'rb') as content_file:
file_content = content_file.read()
list_title = "Documents"
target_folder = ctx.web.lists.get_by_title(list_title).root_folder
name = os.path.basename(path)
target_file = target_folder.upload_file(name, file_content).execute_query()
print("File has been uploaded to url: {0}".format(target_file.serverRelativeUrl))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RaytheonXie-MSFT |
