Create queries
Overview
Queries, also known as filters, are strings that are appended in the request as query parameters. Sitefinity JS SDK allows you to build queries programmatically.
Queries can be built with functions such as skip(), take(), count(), order(), select(), and where().
NOTE: All queries that use the
where()clause are implicitly followed by anand()operand.
For example the queryquery.where().and().endswith("Title", "Record").done().done()produces the string(endswith(Title, 'Record')), which has a set of unused parenthesis - the outermost ones.
Example
This article uses as example a query that returns items whose title ends with Record AND the age of the item is not equal to 15OR the content equals test.
You build and use this query in the following way:
function getListOfNewsItems(sitefinity) {
var dataOptions = {
urlName: "newsitems",
providerName: "OpenAccessDataProvider",
cultureName: "en"
}
//Get the query object
var query = new Sitefinity.Query();
//Create the query
var query = query.where() // there is an implicit and() here
.endsWith("Record", "Title")
.or()
.ne('age', '15')
.eq('Content', 'test')
.done()
.done();
//Build the query
var filter = query.build();
console.log(filer);
//Use the query
var data = sitefinity.data(dataOptions);
data.get({
action: null
query: filter,
fallbackProperties: null,
successCb: function (data) {
console.log(data);
},
failureCb: function (data) {
console.log(data);
}
});
}
Procedure
-
Get the query object
You start by calling theQuery()function of theSitefinityobject.
This function returns aQueryobject that is the entry point for building queries. -
Create the query
The query ANDs the result of theendwithoperator and the result of theoroperand, which holds inside thene(not equal) andeq(equals) operators.The
.done()functions are used to specify where the operation ends. It is required so that the filter can be closed. Each time you use anand()or anor()operand you must add adone(). -
Build the query
After you build the query, you must build it by calling the thebuild()function on the query object. -
Use the query
After you build the query, you can add it to aGETrequest to filter the items.