Copy multiple list items from one list to another using SP services/ Rest or workflows
-
09-01-2021 - |
Question
I am able to use spservices getlistitems to get the list items that I need to copy into a new list.
Is there a way to copy these items to another list using REST API or SPServices or Workflows (looping through)
Please advise
La solution
Use REST API $batch or Rest API BreezeJS. Sample below:
var EmployeesAsJson = [
{
__metadata: {
type: 'SP.Data.EmployeesListItem'
},
FirstName: 'User1',
LastName: 'Test1',
Email: 'User1.Test1@email.com'
},
{
__metadata: {
type: 'SP.Data.EmployeesListItem'
},
FirstName: 'User2',
LastName: 'Test2',
Email: 'User2.Test2@email.com'
},
{
__metadata: {
type: 'SP.Data.EmployeesListItem'
},
FirstName: 'User3',
LastName: 'Test3',
Email: 'User3.Test3@email.com'
}];
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;
// for 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(\'Employees\')'
+ '/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 + '--');
// generate the body of the batch
var 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 + '"'
};
// create request
jQuery.ajax({
url: endpoint,
type: 'POST',
headers: batchRequestHeader,
data: batchBody,
success: function (response) {
alert("Successfully saved a batch request");
},
fail: function (error) {
alert('.. create employee FAIL ', error);
}
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange