Build functions

URI

URI sets in QueryBuilder constructor.

var builder = new Xrm.oData.QueryBuilder('https://services.odata.org/V4/Northwind/Northwind.svc');

Entity

Set Entity name by setEntity() function.

builder.setEntity('Orders');

Columns

To set selected columns use setColumns() function. It takes string with comma-separated columns names.

builder.setColumns('OrderID,CustomerID,ShipCountry');

Expand

If you want select related entities then use setExpand() function. It takes string with comma-separated related entities names.

builder.setExpand('Order_Details');

Filters

You need to use setFilters() function to add filters. Function gets object with type Xrm.oData.FilterInfo.

Xrm.oData.FilterInfo have a constructor that takes object with fields:
  • filterType - enum with values: Xrm.oData.FilterTypes.And and Xrm.oData.FilterTypes.Or;
  • filterExpressions - array of strings with filters expressions;
  • filters - array of objects with type Xrm.oData.FilterInfo.

builder.setFilters(new Xrm.oData.FilterInfo({
	filterType: Xrm.oData.FilterTypes.And,
	filterExpressions: [
		"ShipCountry eq 'Sweden'",
		'OrderID lt 10800'
	],
	filters: [
		new Xrm.oData.FilterInfo({
			filterType: Xrm.oData.FilterTypes.Or,
			filterExpressions: [
				"CustomerID eq 'BERGS'",
				'OrderID gt 10700'
			],
			filters: [],
		}),
	]
}));

Order

You need to use setOrders() function to add filters. Function gets object with type Xrm.oData.OrderColumn.

Xrm.oData.OrderColumn have a constructor that takes object with fields:
  • column - name of column;
  • order - enum with values: Xrm.oData.OrderTypes.Asc and Xrm.oData.OrderTypes.Desc.
builder.setOrders([
	new Xrm.oData.OrderColumn({
		column: 'CustomerID',
		order: Xrm.oData.OrderTypes.Asc
	}),
	new Xrm.oData.OrderColumn({
		column: 'OrderID',
		order: Xrm.oData.OrderTypes.Desc
	})
]);

Skip

Use setSkip() function to skip first rows.

builder.setSkip(10);

Top

Use setTop() function to select N first rows.

builder.setTop(10);

Query string

To build query use toString() function. It is return query URI.

var query = builder.toString();

Execute query

You can execute query by sendQuery() function. It takes two functions as arguments: success callback and error callback.

builder.sendQuery(function(data) {
	for(var i = 0; i < data.value.length; i++) {
		var company = data.value[i];
		for (var prop in company) {
			if (company.hasOwnProperty(prop)) {
			c	onsole.log(prop + ': ' + company[prop]);
			}
		}
		console.log('---');
	}
},
function(error) {
	var data = JSON.parse(error.responseText);
	console.error(data.error.message);
});

Last edited Aug 7, 2016 at 6:20 PM by azaharov, version 1