0

I have a list 'NewsList' with columns Title, ShortDescription,Image ,Programs,Division and Tag.

Here, Programs,Division and Tag are lookup fields with same list name. When I try to retrieve the values, Title and ShortDescription are coming fine. But in Image, Programs,Division and Tag, its displaying [object Object]. I am a beginner to SharePoint and NAPA. Kindly help.

Below is the snippet of the codes I am using.

 function getitems() {
     var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
     var context1 = new SP.ClientContext.get_current();
     hostcontext = new SP.AppContextSite(context1, hostUrl);
     web = hostcontext.get_web();

     var selectedList = "NewsList";
     var selectedListTitle = web.get_lists().getByTitle(selectedList);  
     var camlQuery = new SP.CamlQuery();
     camlQuery.set_viewXml("<View><ViewFields>" +
                           "<FieldRef Name='Title' />" +
                           "<FieldRef Name='ShortDescription' />" +
                           "<FieldRef Name='Image' />" +
                           "<FieldRef Name='Programs' />" +
                           "<FieldRef Name='Division' />" +
                           "<FieldRef Name='Tag' />" +
                           "</ViewFields></View>')");
     listItemCollection = selectedListTitle.getItems(camlQuery);
     context.load(listItemCollection, "Include(Title, ShortDescription, Image, Programs, Division, Tag)");
     context.executeQueryAsync(onGetItemsSuccess, onGetItemsFail);
    }

    function onGetItemsSuccess(sender, args) {
      var listItemEnumerator = listItemCollection.getEnumerator();

      var strTbl = '';
      strTbl = strTbl + '<table><tr><td>Title</td><td>ShortDescription</td><td>Image</td><td>Programs</td><td>Division</td><td>Tag</td></tr>';

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();


        strTbl = strTbl + '<tr><td>' + oListItem.get_item('Title') + '</td><td>' + oListItem.get_item('ShortDescription') + '</td>' + '<td>' + oListItem.get_item('Image') + '</td> +
                            '<td>' + oListItem.get_item('Programs') + '</td><td>' + oListItem.get_item('Division') + '</td>' +
                            '<td>' + oListItem.get_item('Tag') + '</td></tr>';
    }
    strTbl = strTbl + '</table>';
      $('#dvNews').html(strTbl);
     }

Kindly Help.

1

For lookup columns use get_lookupValue() to get the lookup information. Example: oListItem.get_item('Programs').get_lookupValue()

UPDATE

In case lookup field supports multiple values, use this code:

//Get Multiple Lookup Field value
var categoryValues = item.get_item('Categories'); 
for(var i = 0; i < categoryValues.length; i++) {
   var categoryValue = categoryValues[i];
   var catLabel = categoryValue.get_lookupValue();
   var catId = categoryValue.get_lookupId();
}

Source: How to get value of custom lookup field.?

  • Thank you Nadeem. But its still not working. Do I have to add something in caml query also? – Mahatma Aladdin Mar 3 '15 at 10:01
  • Are your fields single lookup type or multi lookup types. Also, yu dont need to use camlQuery.set_viewXml as you are referencing the fields in the load statement. – Nadeem Yousuf Mar 3 '15 at 10:10
  • its single lookup – Mahatma Aladdin Mar 3 '15 at 13:39
  • Are you getting any error. Try using developer tools to debug the code. – Nadeem Yousuf Mar 3 '15 at 13:42

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.