SharePoint REST API - 设置欢迎页面
我正在尝试通过REST API设置SharePoint网站的欢迎页面。以下是我的代码:
def request_digest(self, site_url):
api_call = "/_api/contextinfo"
url = f"{site_url}{api_call}"
headers = {
"Authorization": f"Bearer {str(self.O365_TOKEN)}",
"Accept": "application/json;odata=verbose"
}
response = requests.post(url, headers=headers)
return data['d']['GetContextWebInformation']['FormDigestValue']
def set_homepage(self, site_url):
RequestDigest = self.request_digest(site_url)
api_call = "/_api/Web/RootFolder"
url = f"{site_url}{api_call}"
payload = {
"__metadata": {
"type": "SP.Folder"
},
"WelcomePage": "SitePages/Home.aspx"
}
headers = {
"X-HTTP-Method":"PATCH",
"Authorization": f"Bearer {str(self.O365_TOKEN)}",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"content-length": str(len(json.dumps(payload).encode('utf-8'))),
"X-RequestDigest": RequestDigest,
"IF-MATCH": "*"
}
response = requests.post(url, headers=headers, json=payload)
这是我收到的响应:
{"error": {"code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException", "message": "无效的请求"}}
希望能得到一些帮助。
我尝试过:
搜索资料,但每个来源(其实不多……)都说这个应该可以工作。我甚至用Fiddler检查了请求头和负载,但我发现的唯一不同是我使用的是OAUTH令牌,而不是x-requestdigest。
尝试过有和没有X-RequestDigest头,因为不清楚在使用OAUTH令牌时是否需要包含它(https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints#writing-data-by-using-the-rest-interface)
用requests.patch替代requests.post
其他API调用GET和POST都正常,我可以创建新网站,获取其属性等等……
谢谢,
Ben
1 个回答
0
你可以使用下面的代码来设置欢迎页面
function SetDefaultPage() {
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/rootfolder",
type: "POST",
data: JSON.stringify({
'__metadata': {
// Type that you are modifying.
'type': 'SP.Folder'
},
'WelcomePage': 'SitePages/home.aspx'
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"IF-MATCH": "*",
"X-HTTP-Method": "PATCH",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
}