Skip to content

Instantly share code, notes, and snippets.

@vgrem
Created September 15, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/7b9889d55dab968f971c8c04ff5758b0 to your computer and use it in GitHub Desktop.
Save vgrem/7b9889d55dab968f971c8c04ff5758b0 to your computer and use it in GitHub Desktop.
Demonstrates how to add UserFieldValue for a Group as value in ListItem via SharePoint JSOM API
function addListItem(list, properties) {
let ctx = list.get_context();
let itemCreateInfo = new SP.ListItemCreationInformation();
let listItem = list.addItem(itemCreateInfo);
for (let name in properties) {
listItem.set_item(name, properties[name])
}
listItem.update();
return executeQuery(ctx);
}
function executeQuery(context) {
return new Promise((resolve, reject) => {
context.executeQueryAsync(function () {
resolve();
}, function (sender, args) {
reject(args);
});
});
}
let ctx = SP.ClientContext.get_current();
let group = ctx.get_web().get_associatedMemberGroup();
ctx.load(group);
executeQuery(ctx)
.then(() => {
let groupVal = new SP.FieldUserValue();
groupVal.set_lookupId(group.get_id());
let list = ctx.get_web().get_lists().getByTitle("Tasks");
let taskProps = {
"Title": "Task123",
"AssignedTo": groupVal
}
return addListItem(list,taskProps);
})
.then(() => {
console.log("Saved");
})
.catch((sender,args) => {
console.log(args.get_message());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment