我正在使用office365-rest-api成功地将一个文件上传到SharePoint,但是当我在SharePoint列中设置元数据时,我得到了一个错误。只有当我有一个文件名非常长(大约180个字符)的文件时,才会发生这种情况。使用短文件名的文件工作正常。
我的代码是
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(
我得到的错误是:-
requests.exceptions.HTTPError: 400客户端错误:对url:https://verylongurl+filename/listItemAllFields的请求错误
url +文件名有字符数限制吗?如果是这样的话,为什么它上传的文件是ok的,但只在我设置了列的时候才失败呢?
发布于 2021-11-19 02:22:29
对REST API端点的请求的请求URI的最大长度为4096个字符。如果请求URI超过此长度,您将在响应中看到以下错误消息:
URI length exceeds the configured limit of 4096 characters
您可以参考以下代码将文件上载到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))
https://stackoverflow.com/questions/70017746
复制相似问题