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

Add Member to Office 365 group with JSON/JavaScript

本文关键字:添加 Office 365组 成员 JSON JavaScript 使用      更新时间:2024-06-13

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

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);
        };
    });
};

在您的文档中,您会发现请求标头中需要身份验证令牌。如果没有身份验证令牌,您将收到一个错误:

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

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

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

2.向Graph API添加权限。

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

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 Web部件页面中。

你将在本文中找到的所有细节步骤,我已经测试过了,在我这边工作得很好。