0
votes

I'm trying to create a function which retrieve all selected items in a list in a specific column however i keep getting following error:

items[i].get_item["Titel"] is not a function

However when i use

items[i].id

It returns all the ids of the selected items

How come it cant return by the column Titel?

Here is my code

function GetSelectedItemsID() {

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){

        var ctx = SP.ClientContext.get_current();
        var items = SP.ListOperation.Selection.getSelectedItems(ctx);
        var myItems = '';
        var i;

         for (i in items)
         {
             myItems += ' ' + items[i].get_item("Titel");

         }


         window.alert(myItems);

    });

}
2

2 Answers

2
votes

SP.ListOperation.Selection.getSelectedItems() method returns key/value collection that contains the list items being selected, where:

  • key zero-based integer value
  • value is an object that contains two attributes, id and fsObjType, where id is the ID of the list item, and fsObjType is the type of the item: 0 = list item or document, 1= folder

Since your goal is to retrieve selected list items, the following example demonstrates how to accomplish it:

Get selected list items (SP.ListItem collection) from a list

function getSelectedItems(success,error)
{
    var context = SP.ClientContext.get_current();
    var listId = SP.ListOperation.Selection.getSelectedList(); //selected list Id
    var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context); //selected Items Ids

    var list = context.get_web().get_lists().getById(listId);
    var listItems = [];
    for (idx in selectedItemIds)
    {
        var item = list.getItemById(parseInt(selectedItemIds[idx].id));
        listItems.push(item);
        context.load(item);
    }
    context.executeQueryAsync(
       function() {
          success(listItems);       
       },
       error); 
}

//Usage
getSelectedItems(function(items){
    for (var i =0 ; i < items.length;i++)
    {
       console.log(items[i].get_item('Title'));  
    }  
},function(sender,args){
    console.log('An error occured: ' + args.get_message());
});
0
votes

i guess that get_item() only takes the "StaticName" and not localized display names so i'd suggest trying

items[i].get_item("Title");