1

In a SharePoint hosted App am trying to save data to my list in Office 365 using REST API below is the code. The problem with this code is it works fine for less values passed (eg: my in my item I pass "FirstName":window.Rest.operations.getValue('FirstName') and "LastName":window.Rest.operations.getValue('LastName') in this case data is saved to list), but if I pass more values in my item (as below in code) to my list I get error

Failed to load resource: the server responded with a status of 400 (Bad Request).

Has anyone faced this problem?

   saveDetails:function(){
    var hostwebUrl = window.Rest.operations.getHostWeb();
    var appwebUrl = window.Rest.operations.getAppWeb();
    var listName ='EmployeeDetails';

    var url = appwebUrl+"/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?@target='"+hostwebUrl+"'";
    var item = {"__metadata": { "type": "SP.Data.EmployeeDetailsListItem" },
                "FirstName":window.Rest.operations.getValue('FirstName'),//This returns me string value
                "LastName": window.Rest.operations.getValue('LastName'),
                "EmailAddress": window.Rest.operations.getValue('EmailAddress'),
                "Website": window.Rest.operations.getValue('Website'), //This is Hyperlink or Picture
                "Position": window.Rest.operations.getValue('Position'),
                "BirthDate": window.Rest.operations.getValue('BirthDate'), //This is Date and time
                "Phone": window.Rest.operations.getValue('Phone'),
                "Fax": window.Rest.operations.getValue('Fax'),
                "Relocate": window.Rest.operations.getValue('Relocate'),
                "PreviousCompany": window.Rest.operations.getValue('PreviousCompany'),
                "Reference": window.Rest.operations.getValue('Reference')
                };
     $.ajax({
    url: url,
    type: "POST",
    data:JSON.stringify(item),
    headers: { "accept": "application/json;odata=verbose;",
                "content-type": "application/json;odata=verbose;",
                "content-length":item.length,
                "X-RequestDigest": $("#__REQUESTDIGEST").val() }, // return data format
    success: function (data) {
      alert('Success')
      // alert("Found " + data.d.results.length + " items." + JSON.stringify(data));
    },
    error: function (data) {
        $("#message").html("Failed to read items.");
    }
});

},
  • My guess is that one of the calls to getValue is returning an unexpected value. You should use Fiddler to inspect the request and response to see if you can get more information. telerik.com/fiddler – Rob Windsor Feb 23 '15 at 13:46
  • To piggyback on @RobWindsor's comment, a tricky value might be your Website field. Make sure the return value for the url is an object with the field type, url and description. See this answer for details on updating a hyperlink field. – wjervis Feb 23 '15 at 15:30
  • Thanks @wjervis perfect you are were right it was the URL field making a mess, the link helped and my problem is resolved. Thanks @Rob Windsor, you were right too i was trying to access Url property of Null Website object in getValue(). – Akshay Randive Feb 24 '15 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.