通过 REST 和 Javascript 在 Sharepoint Online 中创建通信站点时出错

Error creating communication site in Sharepoint Online via REST and Javascript

我正在尝试创建一个基于通信网站模板的 Sharepoint 在线网站集,正如 Microsoft 在此处 https://docs.microsoft.com/en-us/sharepoint/dev/apis/communication-site-creation-rest 所描述的那样,方法是在不同的 Sharepoint 网站的页面上使用 javascript

但是,我一直收到 403 的响应 - 虽然我是租户管理员,因此绝对有权创建网站。 我是 运行 下面的脚本,来自 url 的页面,例如 companyname.sharepoint.com/sites/myTestSite/sitepages/RunSomeJS.aspx

我已经尝试指定两者的 REST 端点域 companyname.sharepoint.com 和 公司名称-admin.sharepoint.com 但得到同样的错误。

function DoRestCall(){      
    var body={'parameters':
    {
        "__metadata":{"type":"SP.Publishing.CommunicationSiteCreationRequest"},
        "AllowFileSharingForGuestUsers":false,
        "Classification":"MyTest Communication Site",
        "Description":"Here is my communication site",
        "SiteDesignId":"6142d2a0-63a5-4ba0-aede-d9fefca2c767",
        "Title":"MyTest Communication Site",
        "Url":"https://companyname.sharepoint.com/sites/testSiteName",            
        "lcid":1033
        }
    };
    $.ajax({
        type: 'POST',
        url: "https://companyname.sharepoint.com/_api/sitepages/create",
        contentType: 'application/json',
        processData: false,
        headers:{
            "accept":"application/json;odata=verbose",
            "content-type":"application/json;odata=verbose",
            "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
        },
        data: JSON.stringify(body),
        success: function () 
        {
            alert('CREATION REQUEST SUBMITTED');
        },
        error: function(data){
            alert('failure:' + data.statusText );
        }
    });
}

关于我在这里做错了什么有什么建议吗?

您需要进行如下几项更改:

1) 更改元数据如下:

"__metadata":{"type":"SP.Publishing.PublishingSiteCreationRequest"},

2) 您需要将parameters替换为request

3) 修改端点以创建通信站点,如下所示:

url: "https://companyname.sharepoint.com/_api/sitepages/publishingsite/create",

3) 确保您是 运行 根站点的代码(其 url 类似于 https://companyname.sharepoint.com/sitepages/RunSomeJS.aspx)并且您是该根站点的站点集管理员。

你的最终代码如下:

function DoRestCall(){      
    var body={'request':
    {
        "__metadata":{"type":"SP.Publishing.PublishingSiteCreationRequest"},
        "AllowFileSharingForGuestUsers":false,
        "Classification":"MyTest Communication Site",
        "Description":"Here is my communication site",
        "SiteDesignId":"6142d2a0-63a5-4ba0-aede-d9fefca2c767",
        "Title":"MyTest Communication Site",
        "Url":"https://companyname.sharepoint.com/sites/testSiteName",            
        "lcid":1033
        }
    };
    $.ajax({
        type: 'POST',
        url: "https://companyname.sharepoint.com/_api/sitepages/publishingsite/create",
        contentType: 'application/json',
        processData: false,
        headers:{
            "accept":"application/json;odata=verbose",
            "content-type":"application/json;odata=verbose",
            "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
        },
        data: JSON.stringify(body),
        success: function () 
        {
            alert('CREATION REQUEST SUBMITTED');
        },
        error: function(data){
            alert('failure:' + data.statusText );
        }
    });
}