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

When i try with this code i got error Object doesn't support property or method 'get_item' . Please help me to resolve

function showTitles(urlColumnName) {
    var selectedItemIds = getSelectedItemIds();
    for (var ii = 0; ii < selectedItemIds.length; ii++) {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var lists = web.get_lists();
        var list = lists.getById(SP.ListOperation.Selection.getSelectedList());
        var item = list.getItemById(ii);

        context.load(item, "Title");
        context.executeQueryAsync(Function.createDelegate(this, getAndShowTitle(item, urlColumnName)),
            Function.createDelegate(this, showError));
    }
}

function getAndShowTitle(item, urlColumnName) {
    var title = item.get_item("Title");
    alert(title);
}


function showError(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
share|improve this question
1  
Have you verified that you actually have an item in the item variable? –  Robban1980 Aug 10 '12 at 5:20
1  
var item = list.getItemById(ii); <- Wouldn't ii be 0, afaik no items can have ID 0. –  Anders Aune Aug 10 '12 at 6:08
1  
on the first run of your loop ii will be 0. But your using Javascript and your get an error indicating that your are not working with the type of object your expecting have in the method. Thus receiving the error message that the property or method doesn't exist on that object. That's why you need to verify earlier that you actually have what you expect in the item variable or handle it in the getAndShowTitle function. –  Robban1980 Aug 10 '12 at 6:15

4 Answers 4

I have not tested your code, but i think,

Try with

getAndShowTitle(sender, args)

also, remove arguments form executeQueryAsync of executeQueryAsync method.

You don't need to pass item in argument, if you load item you will get that item in succeed method.

Check for ref: http://msdn.microsoft.com/en-us/library/hh185007.aspx

share|improve this answer

There are some errors in your javascript code. The error you pointed is solved by Vardhaman Deshpande by declaring a global variable "selectedItem" and by shahjinesh11 (the argument is not item, but "sender").

But there are some other improvements you can do:

  • variable ii in the for-loop is just an index, not the id of the selected items
  • don't create context, current list inside the for-loop, it can be instantiated outside the for-loop.
  • "new" is useless in "SP.ClientContext.get_current()"
  • you can't write arguments in the delegate getAndShowTitle: Function.createDelegate(this, getAndShowTitle(item, urlColumnName)
  • To avoid global variables like "selectedItem", use anonymous functions like in my code example.

This code is similar to the code I pasted in my another answer: Read selected item from List

//list
var listGuid = SP.ListOperation.Selection.getSelectedList();
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getById(listGuid);

//selected items
var selectedItems = SP.ListOperation.Selection.getSelectedItems();
var items = [];
for(var i = 0; i < selectedItems.length; i++) {
    var id = selectedItems[i].id;
    var item = list.getItemById(id);
    items.push(item);
    ctx.load(item, "Title");
}
if (items) {
    ctx.executeQueryAsync(function() {
        //just alert for testing
        var titles = "Titles of the selected items:\n";
        for(var j = 0; j < items.length; j++) {
            titles += items[j].get_item("Title") + "\n";
        }
        alert(titles);
    }, function(sender, args){
        alert('Request failed. ' 
            + args.get_message() + '\n' 
            + args.get_stackTrace());
    });
}
share|improve this answer

in the example above, since multiple calls are made, all selectedItem objects will be the exact same object, therefore get_item("Title") will always return the exact same value. The last value.

share|improve this answer

Try this:

var selectedItem;

function showTitles(urlColumnName) {
    var selectedItemIds = getSelectedItemIds();
    for (var ii = 1; ii < selectedItemIds.length; ii++) {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var lists = web.get_lists();
        var list = lists.getById(SP.ListOperation.Selection.getSelectedList());
        selectedItem = list.getItemById(ii);

        context.load(item, "Title");
        context.executeQueryAsync(Function.createDelegate(this, getAndShowTitle),
            Function.createDelegate(this, showError));
    }
}

function getAndShowTitle(sender, args) {
    var title = selectedItem.get_item("Title");
    alert(title);
}


function showError(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

The Success callback of executeQueryAsync method cannot be passed arguments. There are 2 default arguments which are passed to it but neither of them is the item you want. So you will have to declare the item variable outside so that both your showTitles and getAndShowTitle functions can access it.

But be aware that since the executeQueryAsync is asynchronous call, your for loop and the callbacks will run in parallel and not synchronously.

share|improve this answer

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.