1

I have a custom list which I am pulling data from using JavaScript. However for some reason it does not pick up two columns when I call them by their name. I have to use console.log(collListItem.getItemAtIndex(0).get_fieldValues()) in the browser console to find the field values and use those names instead.

What's the reason why it won't pick up these columns? Shouldn't it always pick up the columns when called by name? My problem is that if the someone else wants to change the code they won't know where to look. I would like everything to be consistent.

function GetListItems(SPHostUrlKey){
    var SPContext = new SP.ClientContext.get_current(SPHostUrlKey);
    var web = SPContext.get_web();
    var List = web.get_lists().getByTitle("People List");

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>0</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');

    collListItem = List.getItems(camlQuery); 
    SPContext.load(collListItem);   
    SPContext.executeQueryAsync(GetListItemsSuccess, GetListItemsFail);
}

while (listItemEnumerator.moveNext()){

    var oListItem = listItemEnumerator.get_current();
    title = oListItem.get_item('Title');    //Title of Link
    caption = oListItem.get_item('p06v'); //This is internal name of the 'Caption' column
    url = oListItem.get_item('URL');        //URL of Links

    order = oListItem.get_item('nywb'); //This is internal name of the 'Order' column
    //order = oListItem.get_item('Order');

}//End of While Loop
3

The functions GetListItemsSuccess and GetListItemsFail are missing in your script. But maybe you have intentionally let them away here. The while-clause has to be in the GetListItemsSuccess-function to retrieve the items:

function GetListItemsSuccess() {
    while (listItemEnumerator.moveNext()){

        var oListItem = listItemEnumerator.get_current();
        title = oListItem.get_item('Title');    //Title of Link
        caption = oListItem.get_item('p06v'); //This is internal name of the 'Caption' column
        url = oListItem.get_item('URL');        //URL of Links

        order = oListItem.get_item('nywb'); //This is internal name of the 'Order' column
        //order = oListItem.get_item('Order');

    }//End of While Loop
}

Once I had the problem that I had to set the ViewFields to get the items. So you can try to add the ViewFields-attributes into the camlQuery.set_viewXml:

camlQuery.set_viewXml("<View><ViewFields>" + "<FieldRef Name=\'Title\'/>" + "<FieldRef Name=\'p06v\'/>"  + "<FieldRef Name=\'URL\'/>" + "<FieldRef Name=\'nywb\'/>" + "</ViewFields></View>");

Another option would be to disable the CAML query and try to get all elements so that you are sure to get them first.

Edit: You can retrieve items from list if you are using the column name.

var fieldExist = false;
  while (e.moveNext()) {
   var field = e.get_current();
   if (field.get_title() == "myfieldname") {
     fieldExist = true;
   }
} return fieldExist;

Please refer to this post and its comments: How to retrieve items using column names

| improve this answer | |
  • Yes I left out the success function on purpose. I don't want to use 'p06v' or 'nywb' for the column names, those are internal column names, I want to use the column names in the list, which are 'Caption' and 'Order'. – Mike Mendez Nov 11 '15 at 14:15
  • @MikeMendez I updatet my answer. – mrstar95 Nov 13 '15 at 7:04

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.