0

Please provide a working solution for this, I'm getting 400 bad request error when doing a batch update. I tried Andrew Cornell's blog post in this regard. But couldn't get it to work yet. Below is the code I tried.

        // create the changeset  
        batchContents.push('--changeset_' + changeSetId);
        batchContents.push('Content-Type: application/http');
        batchContents.push('Content-Transfer-Encoding: binary');
        batchContents.push('');
        batchContents.push('POST ' + webUrl + '(' + itemProperties[i].Id + ')' + ' HTTP/1.1'); //+ '(' +itemProperties[i].Id + ')' 

        batchContents.push('Content-Type: application/json;odata=verbose');
        batchContents.push('Accept: application/json;odata=verbose');
        batchContents.push('If-Match: ' + itemProperties[i].__metadata.etag);
        //batchContents.push('X-HTTP-Method:' + 'MERGE');
        batchContents.push('X-RequestDigest:' + $("#__REQUESTDIGEST").val()); 
        batchContents.push('');
        batchContents.push(JSON.stringify(itemUpdater));
        batchContents.push('');
    }
    // END changeset to create data  
    batchContents.push('--changeset_' + changeSetId + '--');

    // generate the body of the batch  
    var batchBody = batchContents.join('\r\n');

    // start with a clean array
    batchContents = new Array();

    // create batch for creating items
    batchContents.push('--batch_' + batchGuid);
    batchContents.push('Content-Type: multipart/mixed; boundary="changeset_' + changeSetId + '"');
    batchContents.push('Content-Length: ' + batchBody.length);
    batchContents.push('Content-Transfer-Encoding: binary');
    batchContents.push('');
    batchContents.push(batchBody);
    batchContents.push('');
    //bath end
    batchContents.push('--batch_' + batchGuid);

    batchBody = batchContents.join('\r\n');


    // create the request endpoint   
    var endpoint = _spPageContextInfo.webAbsoluteUrl
                   + '/_api/$batch';

    // batches need a specific header  
    var batchRequestHeader = {
        'X-RequestDigest': jQuery("#__REQUESTDIGEST").val(),
        'Content-Type': 'multipart/mixed; boundary="batch_' + batchGuid + '"'      
    };
    url: endpointUri,
    type: 'POST',
    async: false,
    headers: batchRequestHeader,
    data: batchBody,

refered : part 1 part 2

yala_cat
  • 309
  • 4
  • 21

0 Answers0