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

时间:2017-10-03 15:41:53

标签: javascript rest sharepoint sharepoint-online

我尝试根据通信网站模板创建一个sharepoint在线网站集,如Microsoft在https://docs.microsoft.com/en-us/sharepoint/dev/apis/communication-site-creation-rest所述,在不同的sharepoint网站的页面上使用javascript

但是,我一直得到403的回复 - 虽然我是租户管理员,所以肯定有权创建网站。 我正在从一个网页上运行下面的脚本,例如companyname.sharepoint.com/sites/myTestSite/sitepages/RunSomeJS.aspx

我尝试过指定两者的REST端点域 companyname.sharepoint.com 和 companyname-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 个答案:

答案 0 :(得分:0)

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

1)更改元数据如下:

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

2)您需要将parameters替换为request

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

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

3)确保您运行的是根网站(其网址类似于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 );
        }
    });
}