Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

ECMAScript & jQuery Deferred - Query Multiple Lists

$
0
0

I'm trying to build a Search page that will search across dozens of lists (all lists use a standard naming based on country). I'm using jQuery Deferred so JS will wait for the functions to end before executing the rest of the code.  It's working but only when I have an Alert popping up in my GetResults function.  I would like to get rid of the Alert and still get the same results.  Once the results from each list are found, they are displayed in a new window in a table.

Below is a short sample of how I'm handling the deferred calls to get the lists based on country and then once that is done, I get the results for each list (full code further below).  The problem is inside getResults.  Without the "Alert" to display which list that I'm querying, it appears to not wait on the results of the query.  When the Alert displays, as long as I don't click it too quickly, it works.  

How do I force it to wait until the SharePoint CAML query is complete before moving on to the next list in the array?

//not real code, just a sample of the main code - see full code below

$.when (

getAllLists()).done (function() {

for each list, call getResults { getResults(thisList).done (function() { }); } Display New Window with htmlTable value }


<script src="/sites/sitename/SiteAssets/Javascript/jquery-1.7.2.min.js" type="text/javascript"></script><script src="/sites/sitename/SiteAssets/Javascript/jquery.SPServices-0.7.1a.min.js" type="text/javascript"></script><script type="text/javascript">

var listItems; var sCountry; var sCountryString; var ctx; var myweb; var allLists; var sYear; var searchType; var searchText; var sortBy; var returnOrder; var htmlTable; var numFound; function StartSearch() { htmlTable = ""; searchText = $.trim($("input[name=searchstring]").val()); if (searchText == "") { alert("A search string is required."); return; } $("input[name=search]").attr('disabled', 'disabled'); allLists = new Array(); sYear = $("input[name='invyear']").val(); if ($("input[name='invcountry']").prop('checked')) { sCountry = "US"; sCountryString = "_US_INV"; } else { sCountry = "Canada"; sCountryString = "_CAN_INV"; } /*build search string based on field values*/ searchType = $("input[name=searchtype]:checked").val(); sortBy = $("input[name=sort]:checked").val(); returnOrder = $("input[name=order]:checked").val(); if (returnOrder == "ASC") { returnOrder = "TRUE"; } else { returnOrder = "FALSE"; } $.when( getAllLists()).done (function() { var len = allLists.length; for (var i = 0; i < len; i++) { var thisList = allLists[i]; getResults(thisList).done (function() { }); } if (htmlTable !="") { var allHTML = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"+"<html xmlns='http://www.w3.org/1999/xhtml'>"+"<head>"+"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"+"<title>Invoice Search Results</title>"+"</head>"+"<body><table width='850' cellpadding='0' cellspacing='0' border='0'>"+ htmlTable + "</table></body>"+"</html>"; tempwindow = window.open('', 'results', 'width=900,resizable=1,scrollbars=1,menubar=1'); tempwindow.document.write(allHTML); } }); } function getAllLists() { var dfd = $.Deferred(function() { ctx = new SP.ClientContext.get_current(); var siteColl = ctx.get_site(); myweb = siteColl.get_rootWeb(); lists = myweb.get_lists(); ctx.load(lists); ctx.executeQueryAsync( function() { var listcount = 0; var listEnum = lists.getEnumerator(); while (listEnum.moveNext()) { list = listEnum.get_current(); var thisTitle = list.get_title(); if (thisTitle.indexOf(sCountryString, 0) > -1) { if (sYear != "") { if (thisTitle.indexOf(sYear, 0) == 0) { allLists[listcount] = thisTitle; listcount = listcount + 1; } } else { allLists[listcount] = thisTitle; listcount = listcount + 1; } } } dfd.resolve(); }, function() { dfd.reject('GetLists for ' + sCountry + ' failed'); } ); }); return dfd.promise(); } function getResults(thisList) { numFound = 0; var dfd = $.Deferred(function() { alert("Querying List: " + thisList); <<<< without this Alert, it fails >>> ctx = new SP.ClientContext.get_current(); var targetList = ctx.get_web().get_lists().getByTitle(thisList); var query = new SP.CamlQuery(); var camlString = "<View><Query><Where><Contains><FieldRef Name='" + searchType + "'/><Value Type='Text'>" + searchText.toLowerCase() + "</Value></Contains></Where><OrderBy><FieldRef Name='" + sortBy + "' Ascending='"+ returnOrder +"'/></OrderBy></Query></View>"; query.set_viewXml(camlString); listItems = targetList.getItems(query); ctx.load(listItems); ctx.executeQueryAsync(function() { var listEnumerator = listItems.getEnumerator(); while (listEnumerator.moveNext()) { var thisID = listEnumerator.get_current().get_item("ID"); var fileRef = listEnumerator.get_current().get_item("FileRef"); /*alert(fileRef);*/ /*alert("Found matching Item in " + thisList + " " + thisID);*/ htmlTable = htmlTable.concat("<tr><td><a href='https://www.mysamplesiteurl.com/Lists/"+ thisList +"/DispForm.aspx?ID=" + thisID + "' target='_blank'>" + listEnumerator.get_current().get_item("Title") + "</a></td><td>"+thisID+"</td><td>" + listEnumerator.get_current().get_item("Invoice_x0020_Date") + "<td>" + listEnumerator.get_current().get_item("Customer") + "</td><td>"+ listEnumerator.get_current().get_item("CustNum")+"</td><td>"+ listEnumerator.get_current().get_item("Total") +"</td></tr>"); numFound = numFound + 1; } }); }); dfd.resolve(); return dfd.promise(); }







Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>