使用SharePoint Online中的REST API批量更新多个项目

时间:2017-08-03 09:05:37

标签: rest sharepoint sharepoint-online batch-updates

请为此提供一个有效的解决方案,我在批量更新时遇到400错误的请求错误。在这方面我试过了Andrew Cornell的博客文章。但还是无法让它发挥作用。以下是我试过的代码。

        // 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,

提到:part 1           part 2

0 个答案:

没有答案