0

I'm trying to copy specific field data from list items to properties in an object, which appears to be working, however, I have multiple rows of data and the function is pasting the same row every time into my array. I'm obviously missing something, but not sure what. Also, if there is a better way to do this I'm all ears! See code below...

function getListData(listName) {
dfd = $.Deferred();
parentId = GetUrlKeyValue("ID", false, location.href);
var context = SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
var list = web.get_lists().getByTitle(listName);
var caml = new SP.CamlQuery();
caml.set_viewXml('<View><Query><Where><Eq><FieldRef Name="LinkID" /><Value Type="Lookup">' + parentId + '</Value></Eq></Where></Query></View>');
listItems = list.getItems(caml);
context.load(listItems, "Include(ID, Current_x0020_Public_x0020_IP_x0, New_x0020_Public_x0020_IP_x0020_, New_x0020_Assigned_x0020_Public_, Disconnect_x0020_IP_x0020_Subnet)");
context.executeQueryAsync(function() {
    var count = listItems.get_count();
    var arraySubnet = [];
    var PublicSubnet = { current: "", new: "", assigned: "" };
    var listEnumerator = listItems.getEnumerator();
    while (listEnumerator.moveNext()) {
        listItem = listEnumerator.get_current();
        id = listItem.get_item("ID");
        currentSubnet = listItem.get_item('Current_x0020_Public_x0020_IP_x0');
        newSubnetRequested = listItem.get_item('New_x0020_Public_x0020_IP_x0020_');
        newAssignedSubnet = listItem.get_item('New_x0020_Assigned_x0020_Public_');
        PublicSubnet.current = currentSubnet;
        PublicSubnet.new = newSubnetRequested;
        PublicSubnet.assigned = newAssignedSubnet;
        arraySubnet.push(PublicSubnet);

    } // End while
    console.log(arraySubnet);
    dfd.resolve();
}, function() {
    dfd.reject();
});
return dfd.promise();
}
0

you are not creating "new" PublicSubnet instances. Instead you are overriding the one instance you have and adding it multiple times.

Move the line var PublicSubnet = { current: "", new: "", assigned: "" }; into the while-loop.

1

Use something like given below inside your While loop:

listItem = listEnumerator.get_current();
var PublicSubnet = {
                     current: listItem.get_item('Current_x0020_Public_x0020_IP_x0'),
                     new: listItem.get_item('New_x0020_Public_x0020_IP_x0020_'),
                     assigned: listItem.get_item('New_x0020_Assigned_x0020_Public_')
                   };
arraySubnet.push(PublicSubnet);
1

Modify the code snippet as below:

var count = listItems.get_count();
var arraySubnet = [];

var listEnumerator = listItems.getEnumerator();
while (listEnumerator.moveNext()) {
    listItem = listEnumerator.get_current();
    id = listItem.get_item("ID");

    currentSubnet = listItem.get_item('Current_x0020_Public_x0020_IP_x0');
    newSubnetRequested = listItem.get_item('New_x0020_Public_x0020_IP_x0020_');
    newAssignedSubnet = listItem.get_item('New_x0020_Assigned_x0020_Public_');
    var PublicSubnet = { current: currentSubnet, new: newSubnetRequested, assigned: newAssignedSubnet };
    arraySubnet.push(PublicSubnet);

} // End while
console.log(arraySubnet);

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.