Is there a JavaScript or jQuery way we can copy a SharePoint list with attachments from one site collection to another?
I exported and imported using Excel but it doesn't bring back the attachments.
Any help much appreciated.
Is there a JavaScript or jQuery way we can copy a SharePoint list with attachments from one site collection to another?
I exported and imported using Excel but it doesn't bring back the attachments.
Any help much appreciated.
You can use below JavaScript code to clone list items from one list to another:
<button type="button" id="buttoninsert" onclick="insert()">Insert</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var sourceListTitle = "Source List Title";
var destListTitle = "Destination List Title";
function insert() {
//copy all items in source list to destination list
getSourceListItems().then(copyItems);
}
function getSourceListItems() {
var deferred = $.Deferred();
var ctx = SP.ClientContext.get_current();
this.sourceList = ctx.get_web().get_lists().getByTitle(sourceListTitle);
this.sourceListFieldCollection = sourceList.get_fields();
this.destList = ctx.get_web().get_lists().getByTitle(destListTitle);
ctx.load(sourceList);
ctx.load(sourceListFieldCollection);
ctx.load(destList);
var items = sourceList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items);
ctx.executeQueryAsync(
function () { deferred.resolve(items); },
function (sender, args) { deferred.reject(sender, args); }
);
return deferred.promise();
}
function logError(sender, args) {
console.log('An error occured: ' + args.get_message());
}
function logSuccess(sender, args) {
console.log('Copied items.');
}
function copyItems(items) {
$.when.apply(items.get_data().forEach(function (sourceItem) { cloneItem(sourceItem); }))
.then(logSuccess, logError);
}
function cloneItem(sourceItem) {
var deferred = $.Deferred();
var ctx = sourceItem.get_context();
var itemCreateInfo = new SP.ListItemCreationInformation();
var targetItem = destList.addItem(itemCreateInfo);
var fieldEnumerator = sourceListFieldCollection.getEnumerator();
while (fieldEnumerator.moveNext()) {
var oField = fieldEnumerator.get_current();
//exclude certain fields
if (!oField.get_readOnlyField() &&
oField.get_internalName() !== "Attachments" &&
!oField.get_hidden() &&
oField.get_internalName() !== "ContentType")
{
var sourceFieldVal = sourceItem.get_item(oField.get_internalName());
if (sourceFieldVal != null) {
targetItem.set_item(oField.get_internalName(), sourceFieldVal);
}
}
}
targetItem.update();
ctx.load(targetItem);
ctx.executeQueryAsync(
function () { deferred.resolve(); },
function (sender, args) { deferred.reject(sender, args);
});
return deferred.promise();
}
</script>
Hope this will help you.
I end up creating an application that does the job using Jquery. It will transfer any attachments from one list to another list across site or site collection.
here is the link.
https://sshareasolutions.com/2019/01/10/transfer-list-attachments-across-site-and-sitecollection/