1

I have several SP 2010 lists with the exact same columns. I would like to perform a union on the lists and display a filtered subset from this union.

I tried doing it using a joined datasource in SP Designer, but I believe the large amount of data makes this not possible (SP Designer keeps crashing when I try to add a dataview using my joined datasource.)

So, I'm trying to do it with SP.js - it works great for one list, but for some reason I can't seem to make it work for multiple lists. I suspect there is something I don't understand about how the Initialize function works.

Anyway, enough preface, here's the code:

  var clientContext = null;
  var web = null;
  ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
  function Initialize()
  {         
    getRegionListData("Migrated 2013");
    getRegionListData("Migrated 2014");
    getRegionListData("Migrated 2011");
    getRegionListData("Migrated 2012");
  }

function getRegionListData(listName)    {

clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        var list = web.get_lists().getByTitle(listName);
        var camlQuery = new SP.CamlQuery();
        var q = "<View><Query><Where><Contains><FieldRef Name='aFOS_x0020_Region' /><Value Type='Text'>A2</Value></Contains></Where></Query></View>";
        camlQuery.set_viewXml(q);
        this.listItems = list.getItems(camlQuery);
        clientContext.load(listItems, 'Include(Id, FACID, Description, aFOS_x0020_Region, EncodedAbsUrl)');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), 
        Function.createDelegate(this, this.onQueryFailed));

}

function onListItemsLoadSuccess(sender, args) {

    var listItemEnumerator = this.listItems.getEnumerator();
    var cgRes;

    while (listItemEnumerator.moveNext())        
{
        var item = listItemEnumerator.get_current();
        var facid = item.get_item('FACID');
var description = item.get_item('Description');
//var afosReg = item.get_item('aFOS_x0020_Region');
var itemId =  item.get_id();
var absUrl = item.get_item('EncodedAbsUrl');
              var url = absUrl.slice(0,69); 
$('#myTable tr:last').after('<tr><td class=wcContainer><a href=' + url + '/DispForm.aspx?ID=' + itemId + '>View</a></td><td class=wcContainer>' + facid + '</td><td class=wcContainer>' + description + '</td></tr>');

}

   var count = this.listItems.get_count();

$('#tdRes').text(count);         
}

function onQueryFailed(sender, args) {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
0

This is what I ended up doing:

itemCount = 0;
runCount = 0;
arrLists = ["Migrated 2011", "Migrated 2012", "Migrated 2013", "Migrated 2014", "Migrated 2015 A - F", "Migrated 2015 G - P"]; 
arrYears = [2011, 2012, 2013, 2014, 2015, 2015];

ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");

function Initialize()
{   
  ctx = new SP.ClientContext.get_current();
  web = ctx.get_web();
  camlQuery = new SP.CamlQuery();
  q = "<View><Query><Where><And><Eq><FieldRef Name='FACID' /><Value Type='Text'>MS007</Value></Eq><And><Neq><FieldRef Name='Status' /><Value Type='Text'>Closed</Value></Neq><Neq><FieldRef Name='Status' /><Value Type='Text'>Work Complete</Value></Neq></And></And></Where></Query></View>";
  camlQuery.set_viewXml(q);     
  while (arrLists[runCount]) {
  getMigratedListData(arrLists[runCount], arrYears[runCount]);
  runCount++;
}   

function getMigratedListData(listName, listYear)     {
    var list = web.get_lists().getByTitle(listName);
    var listItems = list.getItems(camlQuery);
    ctx.load(listItems, 'Include(Id, FACID, Description, EncodedAbsUrl, Request_x0020_ID, Status)');
    ctx.executeQueryAsync(function() {onListItemsLoadSuccess(listItems, listYear);}, onQueryFailed);        
}

function onListItemsLoadSuccess(myItems, listYear) {   
    var listItemEnumerator = myItems.getEnumerator();      
    while (listItemEnumerator.moveNext())        
   {
       var item = listItemEnumerator.get_current();
       var itemId =  item.get_id();
       var reqId = item.get_item('Request_x0020_ID');
       var facid = item.get_item('FACID');
       var description = item.get_item('Description');
       var absUrl = item.get_item('EncodedAbsUrl');
       var status = item.get_item('Status');
       var url = absUrl.slice(0,69);
       $('#dataTable tr:last').after('<tr><td class=wcContainer>' + listYear + '</td><td class=wcContainer><a href=' + url + '/DispForm.aspx? ID=' + itemId + '>' + reqId + '</a></td><td class=wcContainer>' + facid + '</td><td class=wcContainer>' + description + '</td><td class=wcContainer>' + status + '</td></tr>');
   }

    itemCount = itemCount + myItems.get_count();
   $('#tdCount').text(itemCount );   

}

function onQueryFailed(sender, args) {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

<table id="countTable" cellpadding="2" width="200px" border="0">
<tr>
<td class="wcHeader">Total Tickets Found:</td>
<td class="wcContainer" id="tdCount">
</td>
</tr>
</table>
<hr>
<table id="dataTable" cellpadding="2" width="1200px" border="0" >
<tr>
<th class="wcTH" id="svYear">YEAR</th>
<th class="wcTH" id="svRequest">REQUEST</th>
<th class="wcTH" id="svFacid">FACID</th>
<th class="wcTH">DESCRIPTION</th>
<th class="wcTH">STATUS</th>
</tr>
</table>
  • Just to clarify: This line was the key to getting it to work: ctx.executeQueryAsync(function() {onListItemsLoadSuccess(listItems, listYear);}, onQueryFailed); – JimShelley May 26 '16 at 13:37

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.