使用Python3更新Sharepoint 2013

7

我目前正在尝试更新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))
1个回答

1
我使用了你的代码来解决我的问题。
我也遇到了同样的问题。我认为更新数据的传递方式不正确。
传递方式如下:
json_data = {
    "__metadata": { "type": "SP.Data.TasksListItem" },
    "Title": "updated title from Python"
}

json_data 传递给 requests,如下所示:

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来查找类型。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接