SharePoint REST API - 设置欢迎页面

问题描述 投票:0回答:1

我正在尝试使用 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)

这是回复:

{“错误”:{“代码”:“-1,Microsoft.SharePoint.Client.InvalidClientQueryException”,“消息”:“无效请求”}}

如有任何帮助,我们将不胜感激。

我已经尝试过:

谢谢, 本

python sharepoint
1个回答
0
投票

您可以使用以下代码来设置WelcomePage

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");
            }
        });
}
© www.soinside.com 2019 - 2024. All rights reserved.