Skip to content

Instantly share code, notes, and snippets.

@phillipharding
Created September 6, 2021 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipharding/0d31ac2875c68010e8d0b81d3682b9f0 to your computer and use it in GitHub Desktop.
Save phillipharding/0d31ac2875c68010e8d0b81d3682b9f0 to your computer and use it in GitHub Desktop.
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