使用JSON / JavaScript将成员添加到Office 365组

时间:2016-04-12 22:25:50

标签: javascript json sharepoint office365 sharepoint-online

我正在尝试将点击SharePoint(在线)网站中的按钮的用户添加到Office 365组。我知道这可以通过使用Add Member API的JSON来完成。

https://github.com/OfficeDev/microsoft-graph-docs/blob/master/api-reference/v1.0/api/group_post_members.md

然而,在JSON方面,我确实缺乏经验,并且一直搞乱POST功能。这是我目前的代码,逗号之前的一切都工作正常。

function showButton() {

    $('btn-1').on('click', function(event) {
        var userProfileProperties
        var clientContext = new SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
        userProfileProperties = peopleManager.getMyProperties();
        clientContext.load(userProfileProperties);
        clientContext.executeQueryAsync(onSuccess, onFail);

        function onSuccess(){
            accountProperties = userProfileProperties.get_userProfileProperties();
            accountId = accountProperties['msOnline-ObjectId'];
            //JSON Query
            jQuery.ajax({   
                url: "https://mysite.sharepoint.com/groups/groupID/members/$ref";
                method: "POST";
                contentType: "application/json";
                dataType: 'json',
                {
                    "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/" + accountId
                };
            });
        };  
        function onFail(){
            alert(failed);
        };
    });
};

1 个答案:

答案 0 :(得分:0)

在您的文档中,您会在请求标头中找到身份验证令牌。如果没有身份验证令牌,您将收到错误消息:

"code": "InvalidAuthenticationToken", "message": "Bearer access token is empty."

作为解决方案,您可以尝试以下步骤:

1.在Azure AD中注册javascript应用程序并配置您的应用以允许OAuth 2.0隐式授权流。使用OAuth 2.0隐式授权流获取触发器。使用隐式授权,您的应用程序通过将用户发送到用户使用其Office 365凭据登录的授权URL从Azure AD为当前登录用户请求访问令牌,然后使用访问令牌重定向回应用程序在网址中。

2.添加Graph API的权限。

3.在线添加html页面到您的sharepoint(使用资源管理器模式)。

4.编辑html,写下面的函数来获取访问令牌:

 function requestToken() { 
   // Change clientId and replyUrl to reflect your app's values 
   // found on the Configure tab in the Azure Management Portal. 
   // Also change {your_subdomain} to your subdomain for both endpointUrl and resource. 
   var clientId = '3dadb44e-feaa-4158-90f5-e129e15db66d';//ID of your App in Azure
   var replyUrl = 'https://o365e3w15.sharepoint.com/sites/XXX/app.html';  //my sharepoint page that requests 
   //an oauth 2 authentification and data
   //It is also referenced in the REPLY URL field of my App in Azure
   var endpointUrl = 'https://graph.microsoft.com/v1.0/me/messages';
   var resource = "https://graph.microsoft.com/";
   var authServer  = 'https://login.windows.net/common/oauth2/authorize?';  
   //var authServer  =  'https://login.microsoftonline.com/common/oauth2/authorize?';//this works either
   var responseType = 'token'; 

   var url = authServer + 
        "response_type=" + encodeURI(responseType) + "&" + 
        "client_id=" + encodeURI(clientId) + "&" + 
        "resource=" + encodeURI(resource) + "&" + 
        "redirect_uri=" + encodeURI(replyUrl); 

   window.location = url; 
}
  1. 之后,您可以对图api端点进行ajax调用以获取/发布请求,例如,获取当前用户的消息:

    var endpointUrl = "https://graph.microsoft.com/v1.0/me/messages";
    
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", endpointUrl); 
    var myToken = getToken();
    // The APIs require an OAuth access token in the Authorization header, formatted like this: 
    //'Authorization: Bearer <token>'. 
    xhr.setRequestHeader("Authorization", "Bearer " + myToken); 
    
    // Process the response from the API.  
    xhr.onload = function () { 
      if (xhr.status == 200) { 
        //alert('data received');
        var message="";
        var object = JSON.parse(xhr.response); 
        for(i=0;i<object.value.length;i++){
        message+='Subject: ' + object.value[i].subject + '<br>';    
       }
       document.getElementById("results").innerHTML = message;
      } else { } 
    }
    // Make request.
    xhr.send(); 
    
  2. 通过在iframe代码中调用此app.html将其显示到任何SharePoint Webpart页面。

  3. 您将在this article中找到的所有详细步骤,我已经过测试并在我身边正常工作。