Hello Admins,
I have encountered issues with CAML filtering where I still couldn't it get working as I wanted. I have been trying to filter a list with 2 fields, Name and Created Date as screenshots attached below. It is filtered with a Lookup which is based on Status field. Then can filter with 2 fields. I have followed the exact instruction from its documentation but still didn't work for some reason.
2 issues here.
- CAML query somehow couldn't filter specific date in search (text) field eventhough if it's just to filter by 1 field, Created.
- But when I try to filter by 2 fields dynamically in the same search field which wrap them inside "And" tag, it cannot work as it throws errors that cannot perform operation more than 1 field.
Screenshots:
Below is an example code of mine. Not sure what's wrong with it.
// filter List when Search field changes.
fd.field('SearchFilter').$on('change', function () {
searchFilterDT();
});
// search filter function.
function searchFilterDT() {
let searchVar = fd.field('SearchFilter').value;
let currentStatus = fd.field('dd_list1').value;
var filterStatus = dt.filter;
if (filterStatus !== '' || currentStatus !== "All") {
if (searchVar) {
var filterName = "<And>";
// add existing filter value.
filterName += filterStatus;
// filtering conditions.
// created date & name.
filterName += "<Contains><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='TRUE'>" + searchVar + "</Value></Contains>";
filterName += "<Contains><FieldRef Name='RequestorName'/><Value Type='Text'>" + searchVar + "</Value></Contains>";
filterName += "</And>";
} else {
var filterName = '';
}
} else {
if (searchVar) {
// var filterName = "<Or>";
// filterName += "<Contains><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='TRUE'>" + searchVar + "</Value></Contains>";
// filterName += "</Or>";
var filterName = "<Contains><FieldRef Name='RequestorName'/><Value Type='Text'>" + searchVar + "</Value></Contains>";
} else {
var filterName = '';
}
}
// when search field is empty on change, reset list with its original status.
if (filterName === '') {
if (fd.field('dd_list1').value === "Completed") {
filterName = "<Contains><FieldRef Name='RequestStatus'/><Value Type='Text'>Completed</Value></Contains>";
}
if (fd.field('dd_list1').value === "In Progress") {
filterName = "<Contains><FieldRef Name='RequestStatus'/><Value Type='Text'>Assigned</Value></Contains>";
}
if (fd.field('dd_list1').value === "Rejected") {
filterName = "<Contains><FieldRef Name='RequestStatus'/><Value Type='Text'>Rejected</Value></Contains>";
}
if (fd.field('dd_list1').value === "All") {
filterName = "";
}
}
dt.filter = filterName;
dt.refresh();
}
This code section where CAML query wouldn't work at all from your documentation.
var filterName = "<And>";
// add existing filter value.
filterName += filterStatus;
// filtering conditions.
// created date & name.
filterName += "<Contains><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='TRUE'>" + searchVar + "</Value></Contains>";
filterName += "<Contains><FieldRef Name='RequestorName'/><Value Type='Text'>" + searchVar + "</Value></Contains>";
filterName += "</And>";
So, the code works to filter list by Name field, but the main issue here, it won't work filtering by Created field for specific date and multiple fields with code above.
Can you help me how to fix this issue here? Appreciate your help. Thanks.