-1

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.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
user388969
  • 337
  • 1
  • 9
  • 30

2 Answers2

0

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.

Hemant Kabra
  • 870
  • 9
  • 29
  • I have to copy the list between two site collections. i added a new context for the destination function getSourceListItems() { var deferred = $.Deferred(); var ctx = SP.ClientContext.get_current(); var ctx1 = new SP.ClientContext("https://new site collection"); this.sourceList = ctx.get_web().get_lists().getByTitle(sourceListTitle); this.sourceListFieldCollection = sourceList.get_fields(); this.destList = ctx1.get_web().get_lists().getByTitle(destListTitle); ctx.load(sourceList); ctx.load(sourceListFieldCollection); ctx1.load(destList);} doesn't work ?? – user388969 Jul 11 '17 at 13:49
  • I was able to copy the list items but not the attachments. I need to copy the attachments as well. – user388969 Jul 11 '17 at 16:51
0

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/

user388969
  • 337
  • 1
  • 9
  • 30