【问题标题】:batch rest call in sharepoint online在线共享点中的批处理休息调用
【发布时间】:2020-01-20 11:39:51
【问题描述】:

我正在尝试了解批处理休息调用的工作原理。

我在互联网上找不到任何简单的例子。我从https://github.com/andrewconnell/sp-o365-rest 中找到了示例,但无法运行这些示例,或者我还不知道如何运行。我猜您必须将应用程序部署到共享点站点。

鉴于此,我只是在寻找批量/批量添加列表项和更新列表项的最简单示例。另外,如果有人知道我如何使该 git 中的应用程序运行,将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript rest api sharepoint sharepoint-online


    【解决方案1】:

    github项目是一个插件项目,所以需要部署插件项目,然后才能使用。

    您可以从here.查看以下脚本

    我在this thread的测试结果

    (function () {
        jQuery(document).ready(function () {
           jQuery("#btnFetchEmployees").click(function () {
                addEmployees();
            });
        });
    })();
    function addEmployees() {
        var employeesAsJson = undefined;
        employeesAsJson = [
                {
                    __metadata: {
                        type: 'SP.Data.EmployeeInfoListItem'
                    },
                    Title: 'Geetanjali',
                    LastName: 'Arora',
                    Technology: 'SharePoint'
                },
                {
                    __metadata: {
                        type: 'SP.Data.EmployeeInfoListItem'
                    },
                    Title: 'Geetika',
                    LastName: 'Arora',
                    Technology: 'Graphics'
                },
                {
                    __metadata: {
                        type: 'SP.Data.EmployeeInfoListItem'
                    },
                    Title: 'Ashish',
                    LastName: 'Brajesh',
                    Technology: 'Oracle'
                }
        ];
    
        addEmployeeInfoBatchRequest(employeesAsJson);
    }
    
    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;
    };
    
    function addEmployeeInfoBatchRequest(employeesAsJson) {
        // generate a batch boundary
        var batchGuid = generateUUID();
        // creating the body
        var batchContents = new Array();
        var changeSetId = generateUUID();
        // get current host
        var temp = document.createElement('a');
        temp.href = _spPageContextInfo.webAbsoluteUrl;
        var host = temp.hostname;
        // iterate through each employee
        for (var employeeIndex = 0; employeeIndex < employeesAsJson.length; employeeIndex++) {
    
            var employee = employeesAsJson[employeeIndex];
    
            // create the request endpoint
            var endpoint = _spPageContextInfo.webAbsoluteUrl
                           + '/_api/web/lists/getbytitle(\'EmployeeInfo\')'
                           + '/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 ' + endpoint + ' HTTP/1.1');
            batchContents.push('Content-Type: application/json;odata=verbose');
            batchContents.push('');
            batchContents.push(JSON.stringify(employee));
            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('');
    
        // create request in batch to get all items after all are created
        endpoint = _spPageContextInfo.webAbsoluteUrl
                      + '/_api/web/lists/getbytitle(\'EmployeeInfo\')'
                      + '/items?$orderby=Title';
    
    
        batchContents.push('--batch_' + batchGuid);
        batchContents.push('Content-Type: application/http');
        batchContents.push('Content-Transfer-Encoding: binary');
        batchContents.push('');
        batchContents.push('GET ' + endpoint + ' HTTP/1.1');
        batchContents.push('Accept: application/json;odata=verbose');
        batchContents.push('');
    
        batchContents.push('--batch_' + batchGuid + '--');
    
        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
        jQuery.ajax({
            url: endpoint,
            type: 'POST',
            headers: batchRequestHeader,
            data: batchBody,
            success: function (response) {
    
                var responseInLines = response.split('\n');
    
    
            $("#tHead").append("<tr><th>First Name</th><th>Last Name</th><th>Technology</th></tr>");
    
                for (var currentLine = 0; currentLine < responseInLines.length; currentLine++) {
                    try {
    
                        var tryParseJson = JSON.parse(responseInLines[currentLine]);
    
                        $.each(tryParseJson.d.results, function (index, item) {
    
                            $("#tBody").append("<tr><td>" + item.Title + "</td><td>" + item.LastName + "</td><td>" + item.Technology + "</td></tr>");
    
                        });
    
    
                    } catch (e) {
    
                    }
                }
            },
            fail: function (error) {
    
            }
        });
    }
    

    【讨论】:

    • 谢谢!非常有帮助,我已经弄清楚我可以使用你的代码并进行批量插入。
    • 有没有办法批量插入,不使用外接项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多