1

I have been able to use getSelectedItems() without issue so I can perform operations, but from my (hopefully incorrect) understanding, the only other value that is returned is the type of item that's selected (objtype)? I'm not sure how to perform something on getSelectedItems(ctx) to retrieve, for instance, the value in "Name" (ie., SamplePhoto.jpg). I'm thinking I need to use another method to compare the IDs to something which has all of the field info for every file, and then retrieve the name values which have IDs matching those in getSelectedItems(ctx).id

edit: The supplied answers so far do not help since it seems like returning or populating global variables is not possible.

2 Answers 2

4

SP.ListOperation.Selection.getSelectedItems() method returns the list of objects that being selected. The object contains two attributes, id and fsObjType, so it is not SP.ListItem object.

The following example demonstrates how to return selected SP.ListItem collection

Example:

function getSelectedItems()
{
    var dfd = $.Deferred(function () {
       var context = SP.ClientContext.get_current();
       var listId = SP.ListOperation.Selection.getSelectedList(); //get selected list Id
       var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context); //get selected objects

       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() {
            dfd.resolve(listItems);
         },
         function (sender, args) {
               dfd.reject(args.get_message());
         }
       ); 
    });
    return dfd.promise();   
}

//Usage
getSelectedItems().then(function(items){
   var fileNames = []; //save file names 
   for (var i =0 ; i < items.length;i++)
   {
       fileNames.push(items[i].get_item('FileRef'));    
   }


});  
4
  • Since you're using async, how can I store that file name rather than print it? I tried storing it as an existing global variable, but that still didn't work.
    – Dinerdo
    Mar 10, 2014 at 20:13
  • Please check the answer, the example has been updated using deferred approach. Selected items (file name) are saved into fileNames array. Mar 11, 2014 at 11:23
  • I have checked the edits for this answer side by side, and I'm not sure it's reflecting your update. The first array I see listItems [] contains the items information and the second array items [] seems to be referring to the same type of array as listItems [].
    – Dinerdo
    Mar 11, 2014 at 12:50
  • my bad, it seems i forgot to update the answer itself, it has has been updated now Mar 11, 2014 at 14:13
1

You need to call load and executeQueryAsync on the the items returned by getSelectedItems and in the load method you need to pass the internal name of the fields which you want to retrieve. See example below:

var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
    for (var i = 0; i < selectedItems.length; i++) {
        var itemId = selectedItems[i].id;
        GetName(itemId);
    }

    function GetName(itemId) {
        var clientContext = SP.ClientContext.get_current();
        var currentList = clientContext.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
        this.singleItem = currentList.getItemById(itemId);

        clientContext.load(this.singleItem, 'Name');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.OnSucceeded), Function.createDelegate(this, this.OnFailed));
    }

    function OnSucceeded() {
        this.NameValue = this.singleItem.get_item('Name');
    }


    function OnFailed(sender, args) {
        alert('Error occurred: ' + args.get_message());
    }
1
  • Running GetName(304) in console throws the "OnFailed" function all the time despite that ID being visibly connected to an item. Any ideas?
    – Dinerdo
    Mar 7, 2014 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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