This project has moved. For the latest updates, please go here.

MS method of creating list item fails with chrome. How to do the same with SPServices?

Nov 12, 2013 at 8:39 AM
Hi,

I've used the MS recommended way of creating a list item, as specified here:

http://msdn.microsoft.com/en-us/library/hh185011(v=office.14).aspx

I have built this in as a button inside an item display template (a search result item for those unfamiliar), which posts data from the current item to a simple list with two columns.

The relevant bits of code in the display template look like this:
<!--#_
var Tittel = $getItemValue(ctx, "Title");
var StartDato = $getItemValue(ctx, "StartdatoOWSDATE");

function postSurvey()
{
    var siteUrl = '/kursinnhold';
    var context = new SP.ClientContext(siteUrl);
    var myWeb = context.get_web();
    var myList = myWeb.get_lists().getByTitle('testlist');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var listItem = myList.addItem(itemCreateInfo);
    listItem.set_item('Title' , Tittel.value);
    listItem.set_item('Deltakernavn' , StartDato.value);
    listItem.update();
    
    context.executeQueryAsync(Function.createDelegate(this, AddNewItemSuccess) ,Function.createDelegate(this, AddNewItemFail));

    function AddNewItemFail(sender, args)
    {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    function AddNewItemSuccess(sender, args)
    {
        alert('item created: '+listItem.get_id());
    }

}

$( document ).ready(function() {
document.getElementById('test').addEventListener('click', postSurvey, false);
});
 _#-->
<div>
<button id="test">Click here to register</button>
</div>
This works well with internet explorer - the new list item is created and contains the values it gets from the item.

However, in Chrome, I keep getting an error message saying
"Request failed. Unexpected response data from server.
null"

My question: How can I achieve the same using SPServices/jQuery (hoping it will work better with other browsers)?

I was not able to figure out if it's possible from the documentation, but that could possibly be because I am not proficient enough with jQuery.
Nov 12, 2013 at 11:41 AM
A quick update.

I seem to have underestimated how difficult this would be.

This is the equivalent using SPServices, and even better - this works across all browsers!
function CreateNewItem() {
var kursDato = $().SPServices.SPConvertDateToISO({
    dateToConvert: StartDato.value,
    });
    
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        webURL: "/kursinnhold/",
        batchCmd: "New",
        listName: "{98E7E0B1-E8CE-4A79-8414-5C9C030D7969}",
        valuepairs: [["Title", Tittel.value],["Deltakernavn", kursDato]],
        completefunc: function(xData, Status){
            console.log(xData.responseText);
            alert("You have now registered your participation!");
        }
    });
}
Coordinator
Nov 12, 2013 at 1:41 PM
SPServices is much easier to work with the CSOM, IMO. ;+)

M.