As we all know, oData is an open standard for formatting queries to web services. Microsoft implemented oData when they created REST services in SharePoint, but in typical Microsoft fashion, they didn’t implement it completely. What they left out is the ability to query for null or non-null values. Where one might write: ?filter=Title eq null or ?filter=Title ne null the response might be all items or no items or an error without regard to whether any items actually have null Title fields.
So what do we do? It’s CAML to the rescue! So what da heck is that? CAML is the XML-based query language we used in the bad old days of SP2007 and SP2010 to query SharePoint’s SOAP-based web services. SOAP services were all that was offered in SP2007 and while REST was introduced in SP2010, it wasn’t fully fleshed out or particularly useful until SP2013. I learned SharePoint front-end development using SPServices – a wonderful library of tools built by Marc D. Anderson – and CAML was how we wrote our queries. Since we migrated to SP2013, I’ve done almost everything in 2013’s expanded REST services, but CAML is still useful in cases where Microsoft’s implementation of oData leaves something to be desired. The CAML Query Schema can be found here.
Instead of a GET, we’re going to do a POST and the data we are posting is the CAML query. Notice in the code below that we can combine the CAML query with oData as long as they don’t conflict with each other. We are asking via CAML that all returns be sorted by Title and that no returns may have a null values in the Field1 or Field2 fields. At the same time, we are asking via oData that the GetITems service only return the top 25 results that match our query.
function restCallWithCaml() {
var requestData = {
"query":{"__metadata":
{"type":"SP.CamlQuery"},
"ViewXml":"<View>" +
"<Query>" +
"<OrderBy><FieldRef Name='Title' /></OrderBy>" +
"<Where>" +
"<And>" +
"<IsNotNull><FieldRef Name='Field1' /></IsNotNull>" +
"<IsNotNull><FieldRef Name='Field2' /></IsNotNull>" +
"</And>" +
"</Where>" +
"</Query>" +
"</View>"
}
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/GetItems" +
"?$top=25",
type: "POST",
data: JSON.stringify(requestData),
async: false,
contentType: "application/json;odata=verbose",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Content-Type": "application/json; odata=verbose"
},
success: function (data) {
try
{
$.each(data.d.results, function (i, item){
console.log("restCallWithCaml Output: " + item.Title);
});
}
catch (err) {console.log("restCallWithCaml Inner: " + JSON.stringify(err))}
},
error: function (err) {console.log("restCallWithCaml Outer: " + JSON.stringify(err))}
});
}
I have also had issues when querying for certain date fields, though for the life of me, I have yet to see a pattern indicating which date fields will work and which ones won’t. In cases where my date queries don’t work, CAML comes to the rescue.