问题描述
我有一个场景,当在列表中创建一个新项目(团队)时,我正在动态创建一个组。现在我需要根据表单中的人员选择器字段(5人选择器字段)向组添加成员。是否可以使用Rest API / JSOM。
I have an scenario that, when a new item (team) is created in list, I am creating a group dynamically. Now i need to add members to the group based on the people picker fields(5 people picker fields) from the form. Is that possible using Rest API/JSOM.
提前致谢。
推荐答案
您可以使用JSOM和REST API将用户添加到SharePoint组。创建组后,您可以
You can add users to SharePoint group using JSOM as well as REST API. After you have created the group, you can
使用以下示例代码将用户添加到JSOM / REST的Success回调组中。
add the users to the group in the Success callback of JSOM/REST using the below sample code .
JSOM代码示例:
参考
JSOM Code Sample : Reference
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<html> User to be added: <input type="text" id="name" /><br> <br> Sharepoint Group Name : <input type="text" id="groupName" /><br> <br> <br> <input type='button' value='Add User' onclick="AddUserToSharePointGroup();" />
</html>
<script language="javascript" type="text/javascript">
var user;
var spGroup;
function AddUserToSharePointGroup() {
var clientContext = new SP.ClientContext.get_current();
var siteGroups = clientContext.get_web().get_siteGroups();
var web = clientContext.get_web();
spGroup = siteGroups.getByName(document.getElementById('groupName').value);
user = web.ensureUser(document.getElementById('name').value);
var userCollection = spGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(spGroup);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
alert('success');
}
function onQueryFailed() {
alert('Request failed.');
}
</script>
REST API示例:
参考
REST API Sample : Reference
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
这篇关于使用Rest API / JSOM将用户添加到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!