I'm trying to retrieve all the selected items and then get the value in the field name E_x002d_Mail_x0020_1. However there seem to be an issue regarding looping the ctx.executeQueryAsync, since it returns the same row data instead of the different ones and sometimes it returns following error:
Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
What am i doing wrong in order to return all the selected items in in this field E_x002d_Mail_x0020_1?
i've tried to debug by checking whether selectedItemId returns the different id's and it does so it gotta be something with the function loop and the ctx?
<button type="button" id="getlistitemid" value="Get Selected Items ID" onclick="GetSelectedItemsID()" />
<script type="text/javascript">
function GetSelectedItemsID() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
var listGuid = SP.ListOperation.Selection.getSelectedList();
//in this example only first selected item is retrieved
var allSelectedItems = SP.ListOperation.Selection.getSelectedItems();
var item;
var ItemEmails='mailto:';
for (item in allSelectedItems){
var selectedItemId = allSelectedItems[item].id;
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getById(listGuid);
var item = list.getItemById(selectedItemId);
ctx.load(item);
ctx.executeQueryAsync(function() {
var title = item.get_item("E_x002d_Mail_x0020_1");
console.log(title);
ItemEmails += title + ";";
}, function(){alert("error");
});
}
window.location.href = ItemEmails;
});
}
</script>
var selectedItemId = allSelectedItems[item].idit returns the correct from that fielditem. Try giving the second one different name.Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requestedand when i dont it just return the same field values even though i selected two different items.