-1

Im new on SharePoint online and i want "POST" (update my list) with Ajax

First step :

var listName = "Creation de projet";
var url = _spPageContextInfo.webAbsoluteUrl;
var itemId = "18"; // Update Item Id here
var title = "New Updated Title";
updateListItem(itemId, listName, url, title, function () {
    alert("Item updated, refreshing avilable items");
}, function () {
    alert("Ooops, an error occured. Please try again");
});

Second step:

function updateListItem(itemId, listName, siteUrl, title, success, failure) {

var test = GetItemTypeForListName('Creation_x0020_de_x0020_projet');
var itemType = "SP.Data.Creation_x0020_de_x0020_projetListItem";
var item = {
    "__metadata": { "type": test },
    "Title": title
};

    getListItemWithId(itemId, listName, siteUrl, function (data) {

        $.ajax({
          url: data.__metadata.uri,
          type: "POST",
          data: JSON.stringify(item),
          headers: {
              "Accept": "application/json;odata=verbose",                
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "X-HTTP-Method": "MERGE",
              "If-Match": "*"
          },
          success: function (data) {
              success(data);
          },
          error: function (data) {
              failure(data);
          }
    });
    }, function (data) {
        failure(data);
    });
}

function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}

function getListItemWithId(itemId, listName, siteurl, success, failure) {

    var url = siteurl + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=Id eq " + itemId;

    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            if (data.d.results.length == 1) {
                console.log("DATA ITEM", data);
                success(data.d.results[0]);
            }
            else {
                failure("Multiple results obtained for the specified Id value");
            }
        },
        error: function (data) {
            failure(data);
        }
    });
}

And the answer getListItemWithId :

enter image description here Thanx !

0

You are most likely getting an error because you are not supplying the correct listitementitytypefullname.

To check that value , you need to query the below url:

/_api/lists/getbytitle('Creation de projet')?$select=ListItemEntityTypeFullName

After that in your second step, replace it as:

var itemType = "SP.Data.Creation_x0020_de_x0020_projetListItem";

Also i have changed the eTag.

Related discussion - Vadim's excellent answer - SharePoint 2013 REST api to update list item

So try your code as below:

First step:

var listName = "Creation de projet";
var url = _spPageContextInfo.webAbsoluteUrl;
var itemId = "18"; // Update Item Id here
var title = "New Updated Title";

updateListItem(id);

Second step

var itemType = "SP.Data.Creation_x0020_de_x0020_projetListItem";
var item = {
    "__metadata": { "type": itemType },
    "Title": "updated title" //change as necessary, just for test purposes
};

function updateListItem(id){

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Creation de projet')/items(" + listItemId + ")",
        type: "POST",
            data: JSON.stringify(item),
            contentType: "application/json;odata=verbose",
            headers: {
                "Accept": "application/json;odata=verbose",                
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": "*"
            },
            success: function (data) {
                console.log(data);
            },
            error: function (data) {
                console.log(data);
            }
    });
}
  • Now i have 415 (Unsupported Media Type) – Nuno Feb 10 '17 at 14:04
  • sorry my bad, can you check edited code ? – Gautam Sheth Feb 10 '17 at 14:25
  • yes and add a screen ! thx – Nuno Feb 10 '17 at 14:33
  • no sadly :s i dont know why – Nuno Feb 10 '17 at 14:50
  • Have edited and tested the code, can you check again ? I dont know what is there in the getlistitemwithid method, but i dont think its necessary if you already know the id. – Gautam Sheth Feb 10 '17 at 15:30

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.