I am new to SharePoint 2013 app develpoment, so I'm having trouble with some easy (I think it should be easy) REST Call. I got a list for issue tickets, which contains a user field column. In my SP app I created a little form with a user Input field called "Assigned to", so that you can choose via People Picker the Person you want to add to the ticket. Now if you click on send button in the form, the whole entered data should be posted as a new line in my list. This all works, except for the user field. I tried several options, but it still won't work:
var hostweburl;
var appweburl;
// Load the required SharePoint libraries
$(document).ready(function () {
//Get the decoded URLs
hostweburl = GetUrlKeyValue("SPHostUrl");
appweburl = GetUrlKeyValue("SPAppWebUrl");
//resources are in URLs in the form:
//web_url/layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
//load the js files and continue to the successHandler
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js",
function () { $.getScript(scriptbase + "SP.RequestExecutor.js"); }
);
}
)
});
//Function to prepare and issue the request to get SharePoint Data
function execCrossDomainRequest() {
var rest_data = JSON.stringify({
'__metadata': { 'type': 'SP.Data.TicketsListItem' },
'Title': $("#Title").val(),
'AssignedtoId': {"results": []}, //HOW CAN I GET THE USER ID ENTERED IN THE FORM?????
'Issue_x002d_Status': $("#status").val(),
'Priority': $("#prio").val(),
'Category': $("#cat").val() });
//executor: The RequestExecutor object
//Initialize the RequestExecutor with the add-in web URL
var executor = new SP.RequestExecutor(appweburl);
// Issue the call against the add-in web.
// To get the title using REST we can hit the endpoint:
// appweburl/_api/web/lists/getbytitle('listname')/items
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the sucess and error events respectively
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists(guid'F1176C57-5AA4-4E73-9865-0EE5638FA682')/items?@target='" + hostweburl + "'", //use guid of list to make sure there are no errors
method: "POST",
body: rest_data,
headers: {
"Accept": "application/json; odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: successHandler,
error: errorHandler
});
}