使用JSON/JavaScript将成员添加到Office 365组
Add Member to Office 365 group with JSON/JavaScript
我正试图将单击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;
}
之后,您可以对图形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();
通过在iframe标记中调用此app.html,将其显示到任何SharePoint Web部件页面中。
你将在本文中找到的所有细节步骤,我已经测试过了,在我这边工作得很好。
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- 在html Select中添加搜索
- 使用JSON/JavaScript将成员添加到Office 365组
- 如何使用 Office 365 加载项(以前称为 Office 相关应用程序)将功能区和用户可定义的函数添加到 Exce
- 将图像作为多部分/相关 MIME 对象添加到 Outlook with Office Js Addin
- 使用ASP为office插件添加web服务.. NET web api
- 如何访问在Word或Excel中添加的表,并使用Office.js迭代每一行和单元格
- 如何使用office-js以编程方式向工作表中的单元格添加超链接