Nishikant Rath

Nishikant Rath

  • 2.1k
  • 34
  • 2.3k

why SP.ListOperation.Selection.getSelectedList(ctx) is null?

Mar 14 2018 5:03 AM
I want to delete multiple selected list items based on the selection in a listview sharepoint webpart, but the problem is when the page is in draft mode it is extracting the list id & selected item id but whenever I publish the page it gives null value in the liest id & of course by this the selected items id becomes blank.
 
Code : 
 
<button onclick="deleteSelectedItem()">Delete</button>
function deleteSelectedItem() {
var context = SP.ClientContext.get_current();
var listID = SP.ListOperation.Selection.getSelectedList(context);
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
if (selectedItems.length > 0) {
var lstObject = context.get_web().get_lists().getById(listID);
for (var i = 0; i < selectedItems.length; i++) {
this.lstObjectItem = lstObject.getItemById(selectedItems[i].id);
lstObjectItem.deleteObject();
}
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
}
else {
alert("Please Select item !");
}
}
function onSuccess(sender, args) {
alert('Items Deleted');
window.location.href = window.location.href;
}
function onFailure(sender, args) {
alert('Request failed. ' + args.get_message());
Please help me solving this in the published page & explain why it is working in the draft version but not in the published one. 

Answers (3)