我有一个自定义SharePoint Online,来自“新”。 这里有4个人选择器字段,允许多个用户输入。
与每个人员选择器字段相关的4个SP组。
在保存(PreSaveAction)时,我想添加各个SP组中每个人员选取器中的用户。 (使用Javascript / jQuery REST)
有可能吗?
答案 0 :(得分:0)
我遵循以下方法来满足自己的要求:
var AppUsrs =[]; // array to store email ID of multiple users from people picker field
function PreSaveItem(){
//SP group 1
var SecVal = getPickerInputElement('ff171');// As there was multiple people picker fields, I took the position.
for(var k=0;k<SecVal.length;k++) // loop through number of users in people field and add each of them in SP group
{
AddUserToSPGrp(SecVal[k]);
}
return true;
}
function getPickerInputElement(identifier)
{
AppUsrs =[];
var tags = document.getElementsByTagName('DIV');
for (var i=0; i < tags.length; i++)
{
var tempString = tags[i].id;
if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0))
{
var innerSpans = tags[i].getElementsByTagName("SPAN");
for(var j=0; j < innerSpans.length; j++)
{
if(innerSpans[j].id != 'content')
{ AppUsrs.push(innerSpans[j].id.split('|')[2]); // get email ID of users
}
}
return AppUsrs;
}
}
return null;
}
function AddUserToSPGrp(emailIDUsr) {
var clientContext = new SP.ClientContext.get_current();
var siteGroups = clientContext.get_web().get_siteGroups();
var web = clientContext.get_web();
spGroup = siteGroups.getByName('SP group Name');
user = web.ensureUser(emailIDUsr);
var userCollection = spGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(spGroup);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
console.log('success');
}
function onQueryFailed() {
console.log('Request failed.');
}
对于将“其他人”字段中的用户添加到其他SP组中,我重复了相同的操作。