Javascript sharepoint online中的批处理rest调用
我试图理解批处理rest调用是如何工作的 我在网上找不到任何简单的例子。我已经从中找到了示例,但无法运行这些示例,或者我还不知道如何运行。我猜您必须将应用程序部署到sharepoint网站 有鉴于此,我正在寻找批量/批处理中添加列表项和更新列表项的最简单示例。此外,如果有人知道我如何从git运行应用程序,我将非常感激Javascript sharepoint online中的批处理rest调用,javascript,rest,api,sharepoint,sharepoint-online,Javascript,Rest,Api,Sharepoint,Sharepoint Online,我试图理解批处理rest调用是如何工作的 我在网上找不到任何简单的例子。我已经从中找到了示例,但无法运行这些示例,或者我还不知道如何运行。我猜您必须将应用程序部署到sharepoint网站 有鉴于此,我正在寻找批量/批处理中添加列表项和更新列表项的最简单示例。此外,如果有人知道我如何从git运行应用程序,我将非常感激 谢谢。github项目是一个外接程序项目,因此您需要部署外接程序项目,然后才能使用它 您可以从中查看下面的脚本 我的考试成绩很好 (函数(){ jQuery(文档).ready(函
谢谢。github项目是一个外接程序项目,因此您需要部署外接程序项目,然后才能使用它 您可以从中查看下面的脚本 我的考试成绩很好
(函数(){
jQuery(文档).ready(函数(){
jQuery(“#btnFetchEmployees”)。单击(函数(){
增加雇员();
});
});
})();
功能添加员工(){
var employeesAsJson=未定义;
employeesAsJson=[
{
__元数据:{
键入:“SP.Data.EmployeeInfoListItem”
},
标题:“Geetanjali”,
姓氏:“Arora”,
技术:“SharePoint”
},
{
__元数据:{
键入:“SP.Data.EmployeeInfoListItem”
},
标题:“Geetika”,
姓氏:“Arora”,
技术:“图形”
},
{
__元数据:{
键入:“SP.Data.EmployeeInfoListItem”
},
标题:“阿什什”,
姓氏:“布拉赫”,
技术:“甲骨文”
}
];
addEmployeeInfoBatchRequest(employeesAsJson);
}
函数generateUid(){
var d=新日期().getTime();
变量uuid='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxx'。替换(/[xy]/g,函数(c){
var r=(d+Math.random()*16)%16 | 0;
d=数学楼层(d/16);
返回值(c='x'?r:(r&0x7 | 0x8)).toString(16);
});
返回uuid;
};
函数addEmployeeInfoBatchRequest(employeesAsJson){
//生成批边界
var batchGuid=generateuid();
//创造身体
var batchContents=新数组();
var changeSetId=generateuid();
//获取当前主机
var temp=document.createElement('a');
temp.href=_spPageContextInfo.webAbsoluteUrl;
var host=temp.hostname;
//遍历每个员工
对于(var employeeIndex=0;employeeIndex
谢谢!非常有帮助,我已经找到了批处理的方法,我可以使用您的代码并进行批处理插入。有没有办法在不使用外接程序项目的情况下进行批量插入?
(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) {
}
});
}