Difference between QueryByAttribute and QueryExpression
QueryByAttribute and QueryExpression are classes to query the data from CRM. Below are some of the differences we have:
QueryByAttribute:
- QueryByAttribute class is a simple class compared to QueryExpression class. Hence, we can select QueryByAttribute if the query is simple.
- No need to enter the conditions expression, condition operators while designing the criteria for QueryByAttribute.
- It queries the CRM data from specified entity by specifying criteria with set of attributes and value pairs.
- It automatically consider “AND” between specified attributes as it doesn’t support “OR”.
- It supports only “Equal” condition operator.
Example:
It retrieves all the accounts which are related to country India and state Delhi.
QueryByAttribute accQueryByAttr = new QueryByAttribute(“account”);
accQueryByAttr.ColumnSet = new ColumnSet(true);
accQueryByAttr.Attributes.AddRange(“new_country”, “new_state”);
accQueryByAttr.Values.AddRange(“India”, “Delhi”);
QueryExpression
- QueryExpression is an object oriented, strongly typed approach to developing queries against the CRM database.
- We need to specify the conditionExpression and Conditiion Operators while designing the queries.
- QueryExpression is preferable if we want to query the CRM data with complex conditions.
- It supports the complex conditions with “AND” and “OR” operators
- It supports different condition operators like “Beginswith, Doesn’tbeginwith, Endswith,…”
- It supports to retrieve the data based on Link entities.
Example: Below query retrieves all the accounts their names starts with “CRM” and related to India country.
QueryExpression accQueryExp = new QueryExpression(“account”);
accQueryExp.ColumnSet = new ColumnSet(true);
accQueryExp.Criteria.AddCondition(“name”, ConditionOperator.BeginsWith, “CRM”);
accQueryExp.Criteria.AddCondition(“new_country”, ConditionOperator.Equal, “India”);
EntityCollection retrievedAccounts=(EntityCollection)OPCrmService.RetrieveMultiple(accQueryExp);
C# code to Check Whether the User is Part of a Team in MS CRM 2011 using QueryExpression
C# code to Check Whether the User is Part of a Team in MS CRM 2011 using QueryExpression
QueryExpression teamQuery = new QueryExpression(“team”);
ColumnSet teamColumnSet = new ColumnSet(“name”);
teamQuery.ColumnSet = teamColumnSet;
teamQuery.Criteria = new FilterExpression();
teamQuery.Criteria.FilterOperator = LogicalOperator.And;
teamQuery.Criteria.AddCondition(“name”, ConditionOperator.Equal, “<Team Name>”);
teamQuery.AddLink(“teammembership”, “teamid”, “teamid”).AddLink(“systemuser”, “systemuserid”, “systemuserid”).LinkCriteria.AddCondition(“systemuserid”, ConditionOperator.Equal, context.UserId);
teamQuery.Criteria = new FilterExpression();
teamQuery.Criteria.FilterOperator = LogicalOperator.And;
teamQuery.Criteria.AddCondition(“name”, ConditionOperator.Equal, “<Team Name>”);
teamQuery.AddLink(“teammembership”, “teamid”, “teamid”).AddLink(“systemuser”, “systemuserid”, “systemuserid”).LinkCriteria.AddCondition(“systemuserid”, ConditionOperator.Equal, context.UserId);