I am trying to insert data in to person or group field using JSOM
Please find below code.
You have to use alias only, don't use email Id. If you have email id substring it
Ex: var userAlias= userEmailID.substring(0, userEmailID.indexOf("@")));
Now you can use below code.
Now you can use below code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insertNewItem = function () { | |
SP.SOD.executeFunc("sp.js", 'SP.ClientContext', function () { | |
var objClientContext = new SP.ClientContext.get_current(); | |
var objWeb = objClientContext.get_web(); | |
var objUsers = new Array(); | |
objUsers.push(SP.FieldUserValue.fromUser("User Alias")); | |
//You can insert more users | |
objUsers.push(SP.FieldUserValue.fromUser("User1 Alias")); | |
var objList = objWeb.get_lists().getByTitle("MyListTitle"); | |
var itemCreateInfo = new SP.ListItemCreationInformation(); | |
var objListItem = objList.addItem(itemCreateInfo); | |
objListItem.set_item('MyTitle', "Welcome"); | |
objListItem.set_item('Approvers', objUsers); | |
objListItem.update(); | |
objClientContext.executeQueryAsync(onSuccess, onFail); | |
}); | |
} | |
onSuccess = function (response) { | |
console.log("Success"); | |
} | |
onFail = function (sender, args) { | |
console.log("fail"); | |
alert(args.get_stackTrace()); | |
alert(args.get_message()); | |
} |