I am able to fetch single SharePoint List using JavaScript. But here I need to Query other list based on the value fetched from first list. I have also used angularjs for the page.
Find the below JavaScript code for the reference.
table_caption.controller("myTableController", function($scope) {
var context;
var web;
var myList;
var camlQuery;
var colListItem;
// calling the getData function
getData();
//This is getData() function
function getData(){
context = new SP.ClientContext("https://mymysite/sites/example");
web = context.get_web();
myList = web.get_lists().getByTitle('My Data');
camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><RowLimit>500</RowLimit></View>");
colListItem = myList.getItems(camlQuery);
context.load(myList);
context.load(colListItem);
context.executeQueryAsync(ongetAllInfo, onAllInfoFailed);
}
//success function
function ongetAllInfo(){
var resultItems = [];
var listItemEnumerator = colListItem.getEnumerator();
while(listItemEnumerator.moveNext()){
var oListItem = listItemEnumerator.get_current();
var resultItem = {};
resultItem['fN'] = oListItem.get_item('firstName').toString();
resultItem['lN'] = oListItem.get_item('lastName').toString();
resultItem['birDate'] = oListItem.get_item('birthDate').toDateString();
// Need to call the other function here so that I can get the officeId from other function based on the firstName and put in the resultItem.
resultItem['officeId'] = getOfficeId(oListItem.get_item('firstName').toString())
resultItems.push(resultItem);
}
$scope.info = resultItems;
$scope.keys = Object.keys($scope.info[0]);
}
//error function
function onAllInfoFailed(){
alert("Failed to retrieve your data. Try again after sometime.");
}
});
Here is my other function that needs to be called.
function getOfficeId(myId){
var myMainId = myId;
var myTrainingCount = [];
var context = new SP.ClientContext("https://mymysite/sites/example");
var web = context.get_web();
var myList = web.get_lists().getByTitle('Second List');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query></View>");
var colListItem = myList.getItems(camlQuery);
context.load(myList);
context.load(colListItem);
context.executeQueryAsync(function(){
var listItemEnumerator = result.getEnumerator();
while(listItemEnumerator.moveNext()){
var oListItem = listItemEnumerator.get_current();
var office_id = oListItem.get_item('Title');
}
},function(sender,args){
alert('Failed to retrieve nominations');
});
}
The problem here is that my getData function gets executed without getting the officeId from other list. I know that this is causing due to asynchronous calls.
But how can I wait till the other function return the value ?