使用 REST 在 SharePoint Online 中外部共享文件夹 API

External Sharing of Folders in SharePoint Online using REST API

我是 SharePoint Online 和 REST 的新手 API。

我目前正在开发一个 SharePoint 加载项,它在驻留在主机 Web 中的文档库中创建文件夹,并将权限共享给指定的外部用户。我目前正在尝试使用 REST API.

做所有事情

我能够成功创建文件夹,但我目前无法将文件夹共享给外部用户

我在尝试使用主机 Web 上下文时解析以下端点时遇到问题。下面的第一个端点用于解析外部电子邮件,第二个端点用于将文件夹实际共享给外部用户。

[appUrl]/_api/SP.AppContextSite(@target)/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerResolveUser?@target='[hostUrl]'

[appUrl]/_api/SP.AppContextSite(@target)/SP.Web.ShareObject?@target='[hostUrl]'

我收到错误 404 Not FoundCannot find resource for the request SP.UI.ApplicationPages.ClientPeoplerPickerWebServiceInterface.clientPeoplePickerResoveUser。当我试图将它强制为 运行.

时,SP.Web.ShareObject 也会出现同样的错误

下面是我正在使用的javascript

var ShareToRecipients = function(folder, recipient, appUrl, hostUrl){

var url = appUrl = "/_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerResolveUser";
url = WebComponents().getTargetUrl(url, hostUrl);

var sharedDocUrl = hostUrl + "/ExternalShareDocLib/" + foldername;

var checkUser = $.ajax({
    url: url,
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify({
        'queryParams': {
            "__metadata": {'type': "SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters"},
            "AllowEmailAddresses": true,
            "AllowMultipleEntities": false,
            "AllUrlZones": false,
            "MaximumEntitySuggestions": 50,
            "PrincipalSource": 15,
            "PrincipalType": 1,
            "QueryString": recipient
        }
    }),
    headers:{
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $('#__REQUESTDIGEST').val()
    },

    error: function(jqXHR, textStatus){
        myapp.LogAPIError(jqXHR, textStatus)
    }
});

checkUser.success(function(data){
    var user = data.d.ClientPeoplePickerResolveUser;
    var result = JSON.parse(users);

    if(users !== undefined){
        var reqUrl = appUrl + "/_api/SP.Web.ShareObject";
        reqUrl = WebComponents().getTargetUrl(reqUrl, hostUrl);

        var shareCall = $.ajax({
            url: reqUrl,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify({
                "url": reqUrl,
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify({
                    "url": sharedDocUrl,
                    "peoplePickerInput": '[' + user + ']',
                    "roleValue": "1073741827",
                    "groupId": 0,
                    "propagateAcl": false,
                    "sendEmail": true,
                    "includeAnonymousLinkInEmail": true,
                    "emailSubject": "A document folder has been shared to you",
                    "emailBody": "A document folder has been shared to you. The folder name is " + folder                       
                })
            });
        });

        shareCall.success(function(data){
            myApp.LogInformation("Successfully shared " + folder + " to " + recipient);
        });

        shareCall.error(function(jqXHR, textStatus){
            myApp.LogAPIError(jqXHR, textStatus);
        });
    }       
});}

用于生成主机URL端点

var WebComponents = function(){
var getTargeturl = function (url, hostUrl){
    if(hostUrl){
        var api = "_api/";

        var index = url.indexOf(api);
        url = url.slice(0, index + api.length)+
        "SP.AppContextSite(@target)" +
        url.slice(index + api.length);

        var connector = "?";
        if(url.indexOf("?") > -1 && url.indexOf("$") > -1){
            connector = "&";
        }

        url = url + connector + "@target='" + hostUrl + "'";
    }

    return url;
}

return{
    getTargeturl: getTargeturl
}}

您能否帮助验证我是否正确使用端点以及我尝试做的事情是否可行? 感谢您的帮助。

干杯, 尼尔

添加我的解决方案以防其他人遇到此问题。 我能够通过以下方式使用 API:

  1. 删除url = WebComponents().getTargetUrl(url, hostUrl)reqUrl = WebComponents().getTargetUrl(reqUrl, hostUrl)
  2. 通过 AppManifest.xmlTenant 中授予应用 完全控制权