0

I have one requirement where I have to create a list item in List A and B.

I have to use single Rest Call to create the list item.

Is it possible to create the list item using the single REST API call in multiple list?

1
  • I am pretty sure it is not possible using SharePoint REST API, since REST API deals with single resource types. You can create a WEB API to accept the list item parameters and create separate instances in multiple predefined lists in your code.
    – UBK
    May 10, 2019 at 17:32

1 Answer 1

1

SharePoint Online (and on-premises SharePoint 2016 and later) and the Office 365 APIs implement the OData $batch query option, we can use $batch to achieve it.

Example: create item in two lists in one request.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    $("#additemtolist").click(function(){
        addItemBatchRequest();
    });
})
function addItemBatchRequest(){
    var listA="ListA";
    var listB="ListB";

    var batchGuid = generateUUID();
    // creating the body
    var batchContents = new Array();
    var changeSetId = generateUUID();


    // add item to ListA
    var itemA = {
        "__metadata": { "type": GetItemTypeForListName(listA)},
        "Title": "Test"
    };

    var endpointA = _spPageContextInfo.webAbsoluteUrl
                   + '/_api/web/lists/getbytitle(\''+listA+'\')'
                   + '/items';

    // create the changeset
    batchContents.push('--changeset_' + changeSetId);
    batchContents.push('Content-Type: application/http');
    batchContents.push('Content-Transfer-Encoding: binary');
    batchContents.push('');
    batchContents.push('POST ' + endpointA + ' HTTP/1.1');
    batchContents.push('Content-Type: application/json;odata=verbose');
    batchContents.push('');
    batchContents.push(JSON.stringify(itemA));
    batchContents.push('');

    // add item to ListB
    var itemB = {
        "__metadata": { "type": GetItemTypeForListName(listB)},
        "Title": "Test"
    };
    var endpointB = _spPageContextInfo.webAbsoluteUrl
                   + '/_api/web/lists/getbytitle(\''+listB+'\')'
                   + '/items';

    // create the changeset
    batchContents.push('--changeset_' + changeSetId);
    batchContents.push('Content-Type: application/http');
    batchContents.push('Content-Transfer-Encoding: binary');
    batchContents.push('');
    batchContents.push('POST ' + endpointB + ' HTTP/1.1');
    batchContents.push('Content-Type: application/json;odata=verbose');
    batchContents.push('');
    batchContents.push(JSON.stringify(itemB));
    batchContents.push('');

    // END changeset to create data
    batchContents.push('--changeset_' + changeSetId + '--');
    // batch body
    var batchBody = batchContents.join('\r\n');
    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('');

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

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

    var batchRequestHeader = {
        'X-RequestDigest': jQuery("#__REQUESTDIGEST").val(),
        'Content-Type': 'multipart/mixed; boundary="batch_' + batchGuid + '"'
    };

    // create request
    $.ajax({
        url: endpoint,
        type: 'POST',
        headers: batchRequestHeader,
        data: batchBody,
        success: function (response) {
            alert("Successfully saved a batch request");
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
}
// Get List Item Type metadata
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}    
function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return uuid;
}
</script>
<input id="additemtolist" type="button" value="AddItemToList" />

Article: Make batch requests with the REST APIs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.