Callback in SharePoint Javascript - clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed) - return object

Kalpana 291 Reputation points
2022-12-15T07:25:48.147+00:00

Hi

I am trying to get the async query to fill up a dictionary object. The dictionary is a global variable.
Basically, I am trying to get the onQuerySucceeded function to return the value. My code is attached.

I understand I have to use callback, but I just do not know how to go about doing it. The function only has to be triggered on textbox keydown.

var dict = {};  
dict = {  
  Title: {},  
  Name: {},  
  Email: null,  
  AccountName: null,  
  Unit: {},  
  EmploymentCategory: {},  
  Status: {},  
  DateJoin: new Date({}),  
  DateExit: null,  
  ResignDate: null,  
  Country: {},  
  Department: {},  
   'Factory-NonFactory': {},  
  'Department-HOD': {},  
  CostCentre: {},  
  UnitSupervisorAccName: {},  
  HODAccountName: {},  
  Section: {}  
};  
  
  
var empidjstxtbox = document.getElementById(empidjs);  
var empnametxt = document.getElementById(empnamejs);  
empidjstxtbox.addEventListener('keydown', function(event){  
   // alert(event.key);  
     // If the user presses the "Enter" key on the keyboard  
  if (event.key === 'Enter') {  
    // Cancel the default action, if needed  
    event.preventDefault();  
    // Trigger the button element with a click  
   //alert(empidjstxtbox.value);  
   //SearchStaffRecords(siteUrl, '1204');  
  // SearchStaffRecords(siteUrl, '1204'),  
  alert(dict);  
     
   console.log("Whats this" + dict.Name);  
//    console.log(dictval.Name);  
  }  
});  
  
function SearchStaffRecords(siteUrlparam, staffIDval) {  
    var clientContext = new SP.ClientContext(siteUrl);  
    var oList = clientContext.get_web().get_lists().getByTitle('StaffRecords');  
  
    var camlQuery = new SP.CamlQuery();  
    //camlQuery.set_viewXml('<View></View>');  
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + staffIDval + "</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");  
    this.collListItem = oList.getItems(camlQuery);  
  
    clientContext.load(collListItem, 'Include(Id,Title,Name,Email,AccountName,Unit,EmploymentCategory,Status,DateJoin,DateExit,Department,Department_x002d_HOD,Country,UnitSupervisorAccName,ResignDate,Factory_x002d_NonFactory,CostCentre,Section)');  
  
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);  
    
  
  
  
}  
  
  
  
function onQuerySucceeded(sender, args) {  
  
    //var listItemInfo = '';  
    var listItemEnumerator = collListItem.getEnumerator();  
    var listCountry;  
    var listSupervisor;  
    //var listName;  
  
  
    while (listItemEnumerator.moveNext()) {  
        var oListItem = listItemEnumerator.get_current();  
        // listCountry = oListItem.get_item('Country').get_lookupValue();  
  
        dict.Title = oListItem.get_item('Title');  
        dict.Name = oListItem.get_item('Name');  
        dict.Unit = oListItem.get_item('Unit').get_lookupValue();  
        dict.EmploymentCategory = oListItem.get_item('EmploymentCategory').get_lookupValue();  
        dict.Status = oListItem.get_item('Status');  
        dict.DateJoin = oListItem.get_item('DateJoin').toLocaleDateString();  
        dict.DateExit = oListItem.get_item('DateExit');  
        dict.ResignDate = oListItem.get_item('ResignDate');  
        dict.Country = oListItem.get_item('Country').get_lookupValue();  
        dict.Department = oListItem.get_item('Department');  
        dict['Factory-NonFactory'] = oListItem.get_item('Factory_x002d_NonFactory');  
        dict['Department-HOD'] = oListItem.get_item('Department_x002d_HOD').get_lookupValue();  
        dict.CostCentre = oListItem.get_item('CostCentre').get_lookupValue();  
        dict.UnitSupervisorAccName = oListItem.get_item('UnitSupervisorAccName').get_lookupValue();  
        dict.Section = oListItem.get_item('Section').get_lookupValue();  
        dict.Email = oListItem.get_item('Email').get_lookupValue();  
        dict.AccountName = oListItem.get_item('AccountName').get_lookupValue();  
  
  
    }  
  
    alert(dict.AccountName);  
  
    // getLeavelist(listCountry);  
    // var ins = new NF.PeoplePickerApi('#' + lvl1approverjs);  
    // //search for jon doe and add the first result to the people picker  
  
    // ins.search(listSupervisor).done(function (data) {  
    //     // alert('Found');  
    //     ins.add(data[0]);  
  
    // });  
}  
  
function onQueryFailed(sender, args) {  
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
}  
  
Microsoft 365 and Office Development Office JavaScript API
Microsoft 365 and Office SharePoint Server Development
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-12-16T02:24:01.967+00:00

    Hi @Kalpana
    Per my test, you can refer to following js code to return item

    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);  
    function onQuerySucceeded(sender, args) {  
         ........  
         callback(dict);  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.