1

I have a code that read all items from List Airtports. I want to get selected Item from List. How can i change below code to get it done, Please help

function ReadListItem() {

        var listTitle = "Airports";

        //Get the current client context
        context = SP.ClientContext.get_current();

        var airportList = context.get_web().get_lists().getByTitle(listTitle);


        var camlQuery = SP.CamlQuery.createAllItemsQuery();

        this.listItems = airportList.getItems(camlQuery);

        context.load(listItems);


        context.executeQueryAsync(ReadListItemSucceeded, CreateListFailed);


    }

    function ReadListItemSucceeded(sender, args) {

        var itemsString = '';
        var enumerator = listItems.getEnumerator();

        while (enumerator.moveNext()) {

            var listItem = enumerator.get_current();
            itemsString += 'AirportName: ' + listItem.get_item('AirportName') + ' AirportCode: ' + listItem.get_item('AirportCode') + '\n';


        }

        alert(itemsString);

    }
1
  • What do you mean by "get selected item"? A specific item? This currently loops through all items in the list. Can you be more clear on what you are trying to do? Commented Aug 2, 2012 at 13:20

1 Answer 1

1

If you mean a standard list view, you can use SP.ListOperation.Selection.getSelectedItems to get the id of the selected list item. Then you can retrieve the whole item (or a part of properties you need):

Here is an example:

var listGuid = SP.ListOperation.Selection.getSelectedList()
//in this example only first selected item is retrieved
var selectedItemId = SP.ListOperation.Selection.getSelectedItems()[0].id
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getById(listGuid);
var item = list.getItemById(selectedItemId);
ctx.load(item);
ctx.executeQueryAsync(function() {
    var title = item.get_item("Title");
    alert(title);
}, function(){alert("error");});

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.