1

following is my code. I am trying to update a Choice ListITem (dropdownlist) in a Custom list using Rest API. I have debugged the code it goes into success handler but the Item still has the old value.

I am trying to change Status to closed. I have noticed even if I change the Status to Status1 some Field that does not exists in the list it goes to Succeess handler anyway.

var data = GetListItem(); //using Rest Api.
var newStatus = "Closed";
var name = "MyList";
var itemType = "SP.Data.MyListListItem";

        var item = {
            "__metadata": { "type": itemType },
            "Status": newStatus
        };
        var t = JSON.stringify(item);

        $.ajax({
            url: data.__metadata.uri, //I have checked uri is "https://condev.sharepoint.com/_api/Web/Lists(guid'98b0412e-e32f-41f2-b1a4-1324ad65fce9')/Items(2)"
            method: "POST",
            body: JSON.stringify(item), // it is "{\"__metadata\":{\"type\":\"SP.Data.MyListListItem\"},\"Status\":\"Closed\"}"
            headers: { 
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": data.__metadata.etag
            },
            success: function (data) {
                alert("Project status changed to closed");
            },
            error: function (data, errorCode, errorMessage) {
                alert("Rest Api error (upadate listitem)=" + errorMessage);
            }
        });
2

The following example demonstrates how to perform Update operation for a List Item via SharePoint REST API:

// Update Operation
// listName: The name of the list you want to get items from
// siteurl: The url of the site that the list is in. // title: The value of the title field for the new item
// itemId: the id of the item to update
// success: The function to execute if the call is sucesfull
// failure: The function to execute if the call fails
function updateListItem(itemId, listName, siteUrl, itemProperties, success, failure) {
    var itemType = GetItemTypeForListName(listName);
    itemProperties["__metadata"] = { "type": itemType };

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

// READ operation
// itemId: The id of the item to get
// listName: The name of the list you want to get items from
// siteurl: The url of the site that the list is in. 
// success: The function to execute if the call is sucesfull
// failure: The function to execute if the call fails
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) {
                success(data.d.results[0]);
            }
            else {
                failure("Multiple results obtained for the specified Id value");
            }
        },
        error: function (data) {
            failure(data);
        }
    });
}

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

Example

Then, the update operation for list item value of Choice field type would be:

var itemProperties = {
        "Status": 'ChoiceValue'
    }; 
updateListItem(4,'Documents',_spPageContextInfo.webAbsoluteUrl,itemProperties,
   function(){    
   },
   function(error){
    console.log(JSON.stringify(error));
   }
);

References

Manipulating list items in SharePoint Hosted Apps using the REST API

  • thanks, but I don't understa?nd what is differences between my ajax and your – Medes May 15 '14 at 11:31
  • 1
    By default, all requests in jQuery.ajax are sent asynchronously. In my example Update operation is invoked in the success handler of getListItemWithId. – Vadim Gremyachev May 15 '14 at 12:00
  • 1
    Very Nice... very well explained...Thank you for the sharing it. – Diva Feb 14 '18 at 13:58

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.