Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);

    }
share|improve this question
    
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? –  RJ Cuthbertson Aug 2 '12 at 13:20
add comment

1 Answer 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");});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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