python - 使用 Python3 更新 Sharepoint 2013
问题描述
我目前正在尝试更新 Sharepoint 2013 列表。
这是我用来完成该任务的模块。但是,当我运行 post 任务时,我收到以下错误:
"b'{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"无效 JSON。JSON 内容中无法识别令牌。"}}}'"
知道我做错了什么吗?
def update_item(sharepoint_user, sharepoint_password, ad_domain, site_url, sharepoint_listname):
login_user = ad_domain + '\\' + sharepoint_user
auth = HttpNtlmAuth(login_user, sharepoint_password)
sharepoint_url = site_url + '/_api/web/'
sharepoint_contextinfo_url = site_url + '/_api/contextinfo'
headers = {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose',
'odata': 'verbose',
'X-RequestForceAuthentication': 'true'
}
r = requests.post(sharepoint_contextinfo_url, auth=auth, headers=headers, verify=False)
form_digest_value = r.json()['d']['GetContextWebInformation']['FormDigestValue']
item_id = 4991 # This id is one of the Ids returned by the code above
api_page = sharepoint_url + "lists/GetByTitle('%s')/items(%d)" % (sharepoint_listname, item_id)
update_headers = {
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose",
"odata": "verbose",
"X-RequestForceAuthentication": "true",
"X-RequestDigest": form_digest_value,
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE"
}
r = requests.post(api_page, {'__metadata': {'type': 'SP.Data.TestListItem'}, 'Title': 'TestUpdated'}, auth=auth, headers=update_headers, verify=False)
if r.status_code == 204:
print(str('Updated'))
else:
print(str(r.status_code))
解决方案
我将您的代码用于我的场景并解决了问题。
我也面临同样的问题。我认为传递更新数据的方式不正确
像下面这样通过:
json_data = {
"__metadata": { "type": "SP.Data.TasksListItem" },
"Title": "updated title from Python"
}
并传递json_data
给如下请求:
r= requests.post(api_page, json.dumps(json_data), auth=auth, headers=update_headers, verify=False).text
注意:我使用SP.Data.TasksListItem
它,因为它是我的类型。用于http://SharePointurl/_api/web/lists/getbytitle('name')/ListItemEntityTypeFullName
查找类型
推荐阅读
- javascript - Jquery mouseenter 和 mouseleave 在 svg 上没有正确触发
- reactjs - 如何禁用材质 ui 自动完成选项
- c# - 找不到服务的 WCF 端点
- java - 返回 Activity 时检索对象
- python - 如何滚动窗口聚合但保留 Pandas 中的唯一索引列?
- mysql - 在以每秒 2500 的速度对我的 api 进行负载测试时,出现以下错误
- c# - ConnectionString 未初始化 .net 核心
- reactjs - TypeError:this.setAttribute 不是函数
- vhdl - 尝试运行实现时 Vivado 中出现“Opt_Design 错误”
- php - 为什么在 Heroku 上找不到我的课程,而在本地环境中却找不到?