0

I'm using SharePoint 2010. I'm trying to load mutliple selected item from a view in a ribbon on a custom list. I can have the selecteditems but when i try to display a value for an item, i've an exception

undefined is not a function

I found this post and still not working: Cannot get field value of list item through JavaScript

    javascript:   
var context = SP.ClientContext.get_current();
var web = context.get_web();
var items = [];
var lists = web.get_lists();
var listId = SP.ListOperation.Selection.getSelectedList();
var list = lists.getById(listId);
var viewcollection = list.get_views();
var view = list.get_views().getByTitle("All Items");
var vf = [];
var viewFields = view.get_viewFields();
context.load(viewFields);
context.load(viewFields);
context.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(){
    alert("viewfield ok");
    var e = viewFields.getEnumerator();
    while (e.moveNext()) {
        var fieldName = e.get_current();
        vf.push(fieldName);
        var listField = vf.join();
    }
    alert(listField);
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(); 
    alert("debut");

    for (var i in selectedItems) {
        var id = selectedItems[i].id;
        alert(id);
        var item = list.getItemById(id);
        items.push(item);
        context.load(item, "LinkTitle");
    }
    context.executeQueryAsync(OnSuccessLoadItem,OnFailureLoadItem);
    function OnSuccessLoadItem(){
        for (var it in items) {
            //exception is throwed here and the column exist
            alert("success="+it.get_item("LinkTitle"));
        }
    }
    function OnFailureLoadItem(){
        alert("zut");
    }
}
function OnFailure(){
    alert("failure viewfields");
}

-Correction:

i changed the loop for displaying item

for(var cpt=0;cpt<items.length;cpt++){
            alert("success="+items[cpt].get_item("LinkTitle"));

        }
  • Are you getting any errors? What you mean by not working? – Amal Hashim Nov 20 '14 at 13:44
  • i mean that throws undefined exception and i cannot have the value for my items. – user1898765 Nov 20 '14 at 14:04
0

If error is about Include try replacing context.load(item, "Include(Title)") with context.load(item, 'Include(Title)')

  • it doesn't work the executequery fails – user1898765 Nov 20 '14 at 14:09
0

Try this

var context = SP.ClientContext.get_current();
var web = context.get_web();
var items = [];
var lists = web.get_lists();
var listId = SP.ListOperation.Selection.getSelectedList();
var list = lists.getById(listId);
for (var i in selectedItems) {
    var id = selectedItems[i].id;
    var item = list.getItemById(id);
    items.push(item);
    context.load(item, "Include(Title)"); //Add other field you want to include by separated with commad
}
context.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(){
    alert("viewfield ok");
    for (var item in items) {
        alert(item.get_item("Title"));
        //alert(item.get_item("FieldInternalName");
    }
}
function OnFailure(sender, args) {
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
  • i added include but still have the same error – user1898765 Nov 20 '14 at 14:00
  • What is the error? – Amal Hashim Nov 20 '14 at 14:00
  • uncaught referenceError: Include is not defined – user1898765 Nov 20 '14 at 14:02
  • sorry i wrote wrong what you did but now the executequery fail and i enter in the function onfailure – user1898765 Nov 20 '14 at 14:07
  • OnFailure use this to find the exact error alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); - Check updated answer – Amal Hashim Nov 20 '14 at 14:10
0

The came from the loop after loaded items. I changed by a classic loop and now it's working

           function OnSuccessLoadItem(){
        for(var cpt=0;cpt<items.length;cpt++){
            alert("success="+items[cpt].get_item(vf[1]));
        }
    }

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.