Sudhanshu Gupta
Sudhanshu Gupta

Reputation: 1

Sharepoint 2013 JSOM : How to iterate through each item of list (get column values) and get information of its parent lookup list columns as well.

List1 have a lookup column to List2. I need column values for both lists using JSOM. Currently i am getting Column values from List1 for each item within executeAsync call...

var List1 = context.get_web().get_lists().getByTitle('List1');
var List2 = context.get_web().get_lists().getByTitle('List2');
var collListItem = List1.getItems(camlQuery);
context.load(collListItem);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);

function onQuerySucceeded(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();

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

        var listItemProject = oListItem.get_item('ProjectID'); // my lookup column
        var listProjectLookupId = listItemProject.get_lookupId();

var itemProject = List2.getItemById(listProjectLookupId);                               
        var country= null;
        context.load(itemProject);
        context.executeQueryAsync(function () {
            country = itemProject.get_item('Country'); // columns from List2

        });
// using column values from both lists
}

enter code here

But in second Async call i'm getting null values.. may be they are not happening one after another..

Upvotes: 0

Views: 3266

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

I would suggest you to utilize List Joins and Projections to retrieve data from parent/child lists.

Assume the following lists:

  • Contacts - contains Country lookup field (target list: Countries)
  • Countries

Then the following example demonstrates how to retrieve list items from both lists.

Example

function createJoinQuery(joinListTitle,joinFieldName,projectedFields)
{
   var queryText = 
   "<View>" +
       "<Query/>" +
         "<ProjectedFields>";
    for(var idx in projectedFields) {
        queryText += String.format("<Field Name='{0}_{1}' Type='Lookup' List='{0}' ShowField='{1}' />",joinListTitle,projectedFields[idx]);
    }
    queryText +=
         "</ProjectedFields>" +
         "<Joins>" +
             "<Join Type='INNER' ListAlias='{0}'>" +
                "<Eq>" +
                  "<FieldRef Name='{1}' RefType='Id'/>" +
                  "<FieldRef List='{0}' Name='ID'/>" +
                "</Eq>" +
             "</Join>" +
         "</Joins>" +
    "</View>";   
    var qry = new SP.CamlQuery();
    qry.set_viewXml(String.format(queryText,joinListTitle,joinFieldName));
    return qry;
}

function getListItems(listTitle,joinListTitle,joinFieldName,projectedFields,success,error)
{ 
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var list =  web.get_lists().getByTitle(listTitle);
   var items = list.getItems(createJoinQuery(joinListTitle,joinFieldName,projectedFields));

   ctx.load(items);
   ctx.executeQueryAsync(
     function() {
       success(items);  
     },
     error
   );
}

Usage

var listTitle = 'Contacts';
var joinListTitle = 'Countries' 
var joinFieldName = 'Country';
var projectedFields = ['ID','Title'];

getListItems(listTitle,joinListTitle,joinFieldName,projectedFields,
  function(items){
     //print items
     for(var i = 0; i < items.get_count(); i++){
        var item = items.getItemAtIndex(i);
        var contactName = item.get_item('Title');
        var countryName = item.get_item('Countries_Title').get_lookupValue();

     }
  },
  function(sender,args){
      console.log(args.get_message());
  });

Upvotes: 2

Related Questions