Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Camlify Filter Expressions
console.clear();
const isEmptyString = (s) => { return (typeof s === "undefined") || (s === null) || (s.length < 1); };
camlCompareOp = "Or";
camlPredicate = "Contains";
options = {};
options.queryByUniqueIdentifier = "90911";
//options.queryByPayrollNumber = "90919";
//options.queryByLocation = "01-001";
//options.queryByFirstname = "PELLI";
//options.queryByLastname = "MOTTO";
a=[
!isEmptyString(options.queryByUniqueIdentifier) ? `<${camlPredicate}><FieldRef Name="eelhrStaffuseruniqueid"/><Value Type="Text">${options.queryByUniqueIdentifier.trim()}</Value></${camlPredicate}>` : null,
!isEmptyString(options.queryByPayrollNumber) ? `<${camlPredicate}><FieldRef Name="eelhrPayrollnumber"/><Value Type="Text">${options.queryByPayrollNumber.trim()}</Value></${camlPredicate}>` : null,
!isEmptyString(options.queryByLocation) ? `<${camlPredicate}><FieldRef Name="eelhrLocationid"/><Value Type="Text">${options.queryByLocation.trim()}</Value></${camlPredicate}>` : null,
!isEmptyString(options.queryByFirstname) ? `<${camlPredicate}><FieldRef Name="eelhrStaffuserfirstname"/><Value Type="Text">${options.queryByFirstname.trim()}</Value></${camlPredicate}>` : null,
!isEmptyString(options.queryByLastname) ? `<${camlPredicate}><FieldRef Name="eelhrStaffuserlastname"/><Value Type="Text">${options.queryByLastname.trim()}</Value></${camlPredicate}>` : null,
].filter(Boolean);
const camlify = (expressions, op) => {
const ex = [...expressions];
const bundled = [];
while (ex.length > 0) {
t = ex.splice(0, 2);
if (t.length > 1) {
bundled.push(`<${op}>${t.join("")}</${op}>`);
} else {
bundled.push(`${t.join("")}`);
}
}
return bundled;
};
console.log("=BEFORE===========================");
e = [...a];
e.map( (s, i) => console.log(i, s) );
if (e.length > 1) {
do {
e = camlify(e, camlCompareOp);
} while (e.length > 1);
}
console.log("=AFTER===========================");
e.forEach( (s, i) => console.log(i, s) );
console.log("=CAML===========================");
console.log(`..
${e.length > 0 ? "<And>" : ""}
${e.length > 0 ? `${e.join("")}` : ""}
<Eq><FieldRef Name="FSObjType"/><Value Type="Counter">0</Value></Eq>
${e.length > 0 ? "</And>" : ""}
..`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment