Search code examples
jqueryrestweb-partssharepoint-2013splistitem

sharepoint rest services - executeAsync not working


I'm using Sharepoint Rest services to update List Items. To do the update, I'm using the function executeAsync from the SP.RequestExecutor object. The code was working fine, but while I was testing it today, I realized the ListItems weren't updating and the executeAsync function was working fine ( going to the success function and there were no errors).

function ActualizarDatosListaConItemType(urlSitio, nomlista, id, metadata, funcionExito, funcionError,     itemType, esAsync) {
// Prepping our update
var item = $.extend({ "__metadata": { "type": itemType } }, metadata);
var executor = new SP.RequestExecutor(urlSitio);
executor.executeAsync({
                        url: urlSitio + "/_api/web/lists/getbytitle('" + nomlista + "')/items('" + id + "')",
                        type: "POST",
                        contentType: "application/json;odata=verbose",
                        data: JSON.stringify(item),                           
                        headers: {
                                  "Accept": "application/json;odata=verbose",
                                  "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                                  "X-HTTP-Method": "MERGE",
                                  "If-Match": "*"
                                  },
  success: function (data) { funcionExito(data); },
  error: function (data) { funcionError(data); }
 });
}

My code is fine I think, please help T_T.


Solution

  • I've been futzing around with this for a few days and my finding is the SP.RequestExecutor has slightly different parameter names than the normal 'direct' ajax query.

    For example, instead of

    type: "POST"
    

    use

    method: "POST"
    

    and for the actual data package to post, instead of

    data: JSON.stringify(item)
    

    use

    body: JSON.stringify(item)
    

    finally, while the GetByTitle embedded function uses quotes for the title parameter within the parenthesis, the items does not. So instead of

    .../items('123')

    use

    .../items(123)

    For completeness, here is the block that I used to update via rest using javascript across a domain with the SP.RequestExecutor object:

        new SP.RequestExecutor(closureThis.appweburl).executeAsync(
            {
                url: closureThis.appweburl + "/_api/SP.AppContextSite(@target)/web/lists(guid\'" + listIdGuid + "\')/items(" + itemId + ")?@target='" + closureThis.hostweburl + "'",
                method: "POST",
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest": closureThis.getDigestValue(),
                    "X-HTTP-Method": "MERGE",
                    "If-Match": "*"
                },
                body: updateBlock,
                success: function (data) { closureThis.updateDataObjectCallback(data); },
                error: spRequestorErrorHandler
            }
        );
    

    Where closureThis is an object I use to store SharePoint variables and handle callbacks Where updateBlock is the JSON.stringify(data) value

    My updateBlock value looks like this:

    "{\"__metadata\":{\"type\":\"SP.Data.TasksListItem\"},\"Title\":\"First Task\"}"