I have a custom people picker input field which is part of an input form to create list items in a customer sharepoint list.
So, when the user clicks the create item button afte he has filled the form, my JavaScript code should extract all the values from the input form, create a JSON element and use jquery to do a REST call in order to create the item in the list. My problem is that I am unable to put the people picker's value into the JSON element. What format does the people picker value to have in order to be passed via a REST call?
I use the following code the extract the value from my custom people picker field. The function create() is triggered when the user clicks the button.
function create() { getPickerInputElement("peoplePickerField"); } function getPickerInputElement(id) { // Get the people picker object from the page. var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerField_TopSpan; // Get information about all users. var users = peoplePicker.GetAllUserInfo(); // Get the first user's ID by using the login name. return getUserId(users[0].Key); } // Get the user ID. function getUserId(loginName, fn) { var context = new SP.ClientContext.get_current(); this.user = context.get_web().ensureUser(loginName); context.load(this.user); context.executeQueryAsync( Function.createDelegate(null, ensureUserSuccess), Function.createDelegate(null, onFail) ); } function ensureUserSuccess() { var userId = this.user.get_id(); var item = { "__metadata": { "type": "SP.Data.TestListItem" },"Title": $("#title").val(),"Description": $("#description").val(),"People": ?????, //What format of the user/people is required to insert into the JSON element??} var url = SPAppWebUrl +"/_api/web/lists/getbytitle('TestList')/items"; $.ajax({ url: url, type: "POST", contentType: "application/json;odata=verbose", data: JSON.stringify(item), headers: {"Accept": "application/json;odata=verbose", // return data format"X-RequestDigest": $("#__REQUESTDIGEST").val() }, success: function (data) { alert("Item created."); }, error: function (data) { alert("Failed"); } }); }
Inside the function ensureUserSuccess the item (
var item = ...
to be sent via REST is created.
Best,
Fabian