2

In my SharePoint 2013 list , i am trying to get the ids of selected items using javascript . I used below script in content editor web part :

enter image description here

by using this i am able to get the ids . but i have two problems:

  1. the result i am getting is like - 1,1,0 or 1,2,0 if i select items.

  2. I need to get the collection of selected.

  3. I need to select item by clicking on checkbox which is not working although i have put up input type as checkbox id.

1
  • That code works? I see quite a few issues with it – wjervis Mar 1 '17 at 12:08
4

Please mark Sam's answer as the correct one.

Note that on the current ListView page the global ctx is already available,
so a call to get_current() is not required

SP.ListOperations take the default ctx when none is specified.

var selected=SP.ListOperation.Selection.getSelectedItems();

Gets you an Object array

var selectedStr = selected.map(function(item){ // loop all Objects
    return item.id;                            // return the ID
}).join(',');                                  // concatenate to one string 
console.log('selected IDs:',selectedStr);
2

Please use below code.

function clickMethod() {

var ctx = SP.ClientContext.get_current();

var items = SP.ListOperation.Selection.getSelectedItems(ctx);

var mySeletedItems = '';

 var i;

 for (i in items)
 {
      mySeletedItems += '|' + items[i].id;
 }
alert (mySeletedItems);

}

Hope it will works for you.

0

If you want to get the selected items from the list view, you can use the SP.ListOperation to get the selected items using Javascript. Refer this

Also, the below for more references. https://msdn.microsoft.com/en-us/library/office/ff407815(v=office.14).aspx

https://msdn.microsoft.com/en-us/library/office/ff409526(v=office.14).aspx

0

Both code example answers are kinda wrong.

Correct usage

var items = SP.ListOperation.Selection.getSelectedItems() // parameter won't do things

enter image description here

What's wrong with the current answers

-> Samir Khimani

You can't provide a context since the function does not take parameters. enter image description here

-> Danny '365CSI' Engelman

While it is true that the function uses a context, this context neither can be provided as parameter nor is a SP.ClientContext. It's actually the list context of the currently selected list and will also work with mutliple lists on the page. 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.