1

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
});

}
2
  • What is returning your People picker input form? Commented Jul 22, 2016 at 11:02
  • It's the PeoplePicker from the MSDN article posted by Amal Hashim below (ofc without the html elements, which I don't Need)
    – Jay T.
    Commented Jul 22, 2016 at 11:24

1 Answer 1

3

For posting multi user field the format we should use is

FieldName: {"results": [UserId1,UserId2....] }

From people picker control you can get the user ids as explained in the msdn article

https://msdn.microsoft.com/en-us/library/office/jj713593.aspx

4
  • Thanks for the quick response, but as far as I understood the article (and the code posted there) , the user id's coming from this.user.get_id() , and obviously I can't put the method in "results []"
    – Jay T.
    Commented Jul 22, 2016 at 11:10
  • You should use jQuery Promise to load up all the user id's into an array. Once it is finished you can invoke REST call. Commented Jul 22, 2016 at 12:57
  • I'm not familiar with the promise method, but for the first entered id, can't i just write: function ensureUserSuccess() { var id = $('#userId').html(this.user.get_id()); var arr_id = $.makeArray(id); return arr_id; } and then pass the var to the rest?
    – Jay T.
    Commented Jul 25, 2016 at 7:58
  • the examples found on google are quite understandable, but referring to my code i got problems implementing it :/
    – Jay T.
    Commented Jul 25, 2016 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.