3

I have a SharePoint list and a webpart with a button. Following is the case.

  1. User selects multiple items from the list.
  2. When user clicks on the button of the webpart, I would like to know the detail of the selected items.
3

You can use the SP.ListOperation.Selection class getSelectedItems() method to get the selected list items. You will get the IDs of the selected list items based on which you can do the further processing.

Here is a good post. You might also want to consider going through this post.

1

SP.ListOperation.Selection Class provides static methods for selecting lists or list items.

SP.ListOperation.Selection.getSelectedItems() method returns the list of items being selected. Selected list item contains the following properties:

  • id - list item id
  • fsObjType - file system object type

The following example demonstrates how to retrieve selected list items:

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 items
for (idx in selectedItemIds)
{
    var itemId = (parseInt(selectedItemIds[idx].id));
    var isListItem = (selectedItemIds[idx].fsObjType == "0");
    //..
}

The next example demonstrates how to retrieve selected SP.ListItem object collection:

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){
  for (var i =0 ; i < items.length;i++)
  {
   var fileUrl = items[i].get_item('FileRef');
   console.log(fileUrl);
  }
});  
  • Is there a way to implement it on the server side using C#? – Anh Nguyen Sep 11 '14 at 4:05

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.