For those looking to transfer list items from one list to another, you can utilize the code snippet provided below:
<button type="button" id="copyButton" onclick="copyListItems()">Copy Items</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var sourceListName = "Source List Name";
var destinationListName = "Destination List Name";
function copyListItems() {
// execute function to duplicate all items from source list to destination list
getSourceListData().then(copyItemsToDestination);
}
function getSourceListData() {
var deferred = $.Deferred();
var context = SP.ClientContext.get_current();
this.sourceListObj = context.get_web().get_lists().getByTitle(sourceListName);
this.sourceFields = sourceListObj.get_fields();
this.destinationListObj = context.get_web().get_lists().getByTitle(destinationListName);
context.load(sourceListObj);
context.load(sourceFields);
context.load(destinationListObj);
var elements = sourceListObj.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(elements);
context.executeQueryAsync(
function () { deferred.resolve(elements); },
function (sender, args) { deferred.reject(sender, args); }
);
return deferred.promise();
}
function logMistake(sender, args) {
console.log('An error has occurred: ' + args.get_message());
}
function logAchievement(sender, args) {
console.log('List items successfully copied.');
}
function copyItemsToDestination(items) {
$.when.apply(items.get_data().forEach(function (srcItem) { transferItem(srcItem); }))
.then(logAchievement, logMistake);
}
function transferItem(srcItem) {
var deferred = $.Deferred();
var context = srcItem.get_context();
var newListItemInfo = new SP.ListItemCreationInformation();
var targetListItem = destinationListObj.addItem(newListItemInfo);
var fieldEnum = sourceFields.getEnumerator();
while (fieldEnum.moveNext()) {
var field = fieldEnum.get_current();
if (!field.get_readOnlyField() &&
field.get_internalName() !== "Attachments" &&
!field.get_hidden() &&
field.get_internalName() !== "ContentType")
{
var srcFieldValue = srcItem.get_item(field.get_internalName());
if (srcFieldValue != null) {
targetListItem.set_item(field.get_internalName(), srcFieldValue);
}
}
}
targetListItem.update();
context.load(targetListItem);
context.executeQueryAsync(
function () { deferred.resolve(); },
function (sender, args) { deferred.reject(sender, args);
});
return deferred.promise();
}
</script>
Related Links:
Guide on Copying SharePoint List Items Between Lists
Using REST/JSON to Duplicate List Items