Push attributes to array, pull attributes from array

Ghoztrider Source

I'm trying to pack multiple attributes from SharePoint list items into an array (selected items only in a view only), pass the array to a function and unpack the multiple attributes to create list items in another list, in short, I'm trying to copy certain fields from list items from one list to another.

The issue is I'm not sure how to pack the attributes from the getselected function and unpack them in the writeSelected function. I've spent a good amount of time scouring the internet and stack sites for information on how to do this with no success. At best my results have come back undefined...

//Get selected items from view
function getSelected() {
    listName2 = "Customer Locations";
    var Astate, Aname;
    var ctx = SP.ClientContext.get_current();
    var clientContext = new SP.ClientContext(); 
    var targetList = clientContext.get_web().get_lists().getByTitle(listName2);
    var SelectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
    var items = [];
    for (var i in SelectedItems) {
        var id = SelectedItems[i].id;
        var item = targetList.getItemById(id);
        clientContext.load(item, "A_x0020_State", "A_x0020_Name");
        items.push(item);
    } // End for
    clientContext.executeQueryAsync(function() {
        var results = [];
        for (var i = 0; i < items.length; i++) {
            items.Aname = item.get_item('A_x0020_Name');
            items.Astate = item.get_item('A_x0020_State');
            results.push( {A_name: results[items.Aname], A_state: results[items.Astate]} );
            console.log(results.A_name);
        } //End for
    }, failure); //End ClientContext
} //End getSelected

//Write selected items to Sites list in WCAP
function writeSelected(results) {
    listName4 = "Sites";
    for (var k in results) {
        var clientContext = SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle(listName4);
        var item = new SP.ListItemCreationInformation();
        var oListItem = oList.addItem(item);
        oListItem.set_item("Link_x0020_ID", fieldValue);
        oListItem.set_item("A_x0020_Name", ???);
        oListItem.set_item("A_x0020_State", ???);
        oListItem.update();
        clientContext.load(oListItem);
        clientContext.executeQueryAsync(success, failure)
    } //End for
} //End writeSelected
javascriptarrayssharepoint

Answers

answered 6 months ago LZ_MSFT #1

If you want to copy list items from one list to another, the following code for your reference:

<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>

Reference:

How to copy SharePoint list items from one list to another

Copy List items using REST/JSON

comments powered by Disqus