2

I need to create an HTML form with two text boxes and a button that you click the update button it inserts the two values ​​in a sharepoint list. Below an example of what I'm trying to do:

// Function to save data in SharePoint list
     PostFormBuilder.RESTPost = function (listTitle, query) {
        var execute = function (listTitle, query) {
            var restUrl = _spPageContextInfo.webServerRelativeUrl +
                "/_api/web/lists/getByTitle('" + listTitle + "')/items";
            if (query != "") {
                restUrl = restUrl + "?" + query;
            }
            var deferred = $.ajax({
                url: restUrl,
                method: "POST",
                body: { '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'Test'},
                headers: {
                    Accept: "application/json;odata=verbose",
                    "Content-Type": "application/json;odata=verbose",
                    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
                    "IF-MATCH": "*",
                    "X-Http-Method": "MERGE"
                }
            });

            return deferred.promise()
        };

        return {
            execute: execute
        }
    }();

I think the problem is in the ajax part but i dont know how fix. Any help?

2

Honestly, you may find using the SPServices javascript library easier to use. Mark Anderson has done a fantastic job handling all the nuances involved when using client side code. You can find the library here . The function you would want to use is UpdateListItems and this discussion thread will show you what you need to do. Hope this helps!

  • 1
    Keep in mind though that SPServices relies on a deprecated feature (SOAP services), so using the REST services sounds like a good idea moving forward. – Christophe Jan 10 '14 at 0:50
  • As much as I like SPServices, it's not what this post is tagged with. It's tagged as a REST question and should be answered that way imo. Christophe has a tiny library that wraps REST: sprest.codeplex.com – iOnline247 Jan 10 '14 at 3:07
  • @iOnline247 thanks for the mention :-) ... but my SPrest library applies to SP 2010 REST, not the 2013 one. – Christophe Jan 10 '14 at 3:24
  • Didn't know that... I'm a JSOM kind of guy. ;) – iOnline247 Jan 10 '14 at 5:54
0

The body property in your ajax call doesn't sound right. I think what you actually want is to pass a string as a data property.

Your Answer

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

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