Team,
I have 3 lists, (a) Issues (b) Risks (c) Incidents. I want to...
1 - Run a CAML query against (a) Issues list to get all the issues not closed. 2 - then run a loop through the titles, and use the title in a second CAML query to get all the (b) risks that match the current risk title in the first loop. 3 - add each matching risk into a string. 4 - after the loop is completed add the string to the issues list for the current issues list item in that cycle of the loop. 5 - do steps 2-4 with incidents i.e. while looping through the (a) issues list items, two different CAML queries will run, one for risks and another for incidents.
This page: http://www.sharepointnadeem.com/2014/10/sharepoint-using-deferredspromises-or.html
shows how to use a promise to sequence asynchronous CAML queries.
[note: the code doesn't do the string creation or list item updates yet, just trying to get the looping to work with the 3 lists first]
but this...
<script type="text/javascript">
$(document).ready(function() {
//don't exectute any jsom until sp.js file has loaded.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updateIssues);
});
// display Issues
function updateIssues() {
getIssueItemsWithCaml('SMOIFIssues').then(
function (issueItems) {
var issueListItemEnumerator = issueItems.getEnumerator();
while (issueListItemEnumerator.moveNext()) {
var issueListItem = issueListItemEnumerator.get_current();
// log item details
console.log('ID: ' + issueListItem.get_item('ID') + ', Title:' + issueListItem.get_item('Title'));
// update matching incidents
getIncidentItemsWithCaml('SMOIFIncidents',issueListItem.get_item('Title')).then(
function (incidentItems) {
var incidentListItemEnumerator = incidentItems.getEnumerator();
while (incidentListItemEnumerator.moveNext()) {
var incidentListItem = incidentListItemEnumerator.get_current();
// log item details
console.log('Issue: ' + issueListItem.get_item('Title') + ', Incident: ' + incidentListItem.get_item('Title') + ', Type:' + incidentListItem.get_item('Account_x003a_AccountTypeDisplay').get_lookupValue());
} console.log('Display of all incidents for ' + issueListItem.get_item('Title') + ' is done.');
},
function (sender, args) {
console.log('An error occured while displaying list items:' + args.get_message());
}
);
// retrive matching risks
} console.log('Issue with status not completed is done.');
},
function (sender, args) {
console.log('An error occured while displaying list items:' + args.get_message());
}
);
}
function getIncidentItemsWithCaml(listTitle2,issueItem2) {
console.log('getIncidentItemsWithCaml: listTitle - ' + listTitle2 + ', issueItem - ' + issueItem2);
//use of $.Deferred in the executeQueryAsync delegate allows the consumer of this method to write 'syncronous like' code
var deferred2 = $.Deferred();
var clientContext2 = new SP.ClientContext.get_current();
var list2 = clientContext2.get_web().get_lists().getByTitle(listTitle2);
var camlQuery2 = new SP.CamlQuery();
camlQuery2.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Issue" /><Value Type="LookupMulti">' + issueItem2 + '</Value></Eq></Where></Query></View>');
var items2 = list2.getItems(camlQuery2);
clientContext2.load(items2);
clientContext2.executeQueryAsync(
Function.createDelegate(this,
function () { deferred2.resolve(items2); }),
Function.createDelegate(this,
function (sender, args) { deferred2.reject(sender, args); }));
return deferred2.promise();
};
function getIssueItemsWithCaml(listTitle) {
//use of $.Deferred in the executeQueryAsync delegate allows the consumer of this method to write 'syncronous like' code
var deferred1 = $.Deferred();
var clientContext = new SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle(listTitle);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Neq><FieldRef Name="issueStatus" /><Value Type="Choice">Completed</Value></Neq></Where></Query></View>');
var items = list.getItems(camlQuery);
clientContext.load(items);
clientContext.executeQueryAsync(
Function.createDelegate(this,
function () { deferred1.resolve(items); }),
Function.createDelegate(this,
function (sender, args) { deferred1.reject(sender, args); }));
return deferred1.promise();
};
</script>
produced this...
ID: 1, Title: [issue title]
getIncidentItemsWithCaml: listTitle - SMOIFIncidents, issueItem - [issue title]
.
.
.
ID: [last ID in issue list], Title: [last issue title]
getIncidentItemsWithCaml: listTitle - SMOIFIncidents, issueItem - [last issue title]
Issue with status not completed is done.
Issue: [last issue title], Incident: [incident#1], Type:[incident type]
Display of all incidents for [last issue title]
Issue: [last issue title], Incident: [incident#2], Type:[incident type]
Display of all incidents for [last issue title]
The console output shows it loops through all the issue list items, then outputs the incidents. And it doesn't filter the incidents based on the title.
again, my goal is;
- Issue 1
- matching incidents based on title of issue 1
- matching risks based on title of issue 1
- Issue 2
- matching incidents based on title of issue 2
- matching risks based on title of issue 2
etc
regards Michael