Using the QueryByAttribute Class
![]() |
[Applies to: Microsoft Dynamics CRM 4.0]
The QueryByAttribute class provides an easy way to build queries that test a set of attributes against a set of values. Use this class together with the RetrieveMultiple method or the RetrieveMultiple message.
The following table lists the properties that you set to create a query expression by using the QueryByAttribute class.
| Property | Description |
| EntityName | Specifies which kind of entity will be retrieved. A query expression can only retrieve a collection of one entity type. |
| ColumnSet | Specifies the set of attributes (columns) to retrieve. |
| Attributes | Specifies the set of attributes selected in the query. |
| Values | Specifies the attribute values to look for when the query is executed. |
| Orders | Specifies the order in which the entity instances are returned from the query. |
| PageInfo | Specifies the number of pages and the number of entity instances per page returned from the query. |
Example
The following code example shows how to use the QueryByAttribute class.
[C#]
// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create a column set holding the names of the columns to be retrieved.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {"name", "accountid"};
// Create the query object.
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = cols;
query.EntityName = EntityName.account.ToString();
// The query will retrieve all accounts whose address1_city is Sammamish.
query.Attributes = new string [] {"address1_city"};
query.Values = new string [] {"Sammamish"};
// Execute the retrieval.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
[Visual Basic .NET]
' Set up the CRM Service.
Dim token As New CrmAuthenticationToken()
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";
Dim service As New CrmService()
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials
' Create a column set holding the names of the columns to be retrieved.
Dim cols As New ColumnSet()
cols.Attributes = New String() {"name", "accountid"}
' Create the query object.
Dim query As New QueryByAttribute()
query.ColumnSet = cols
query.EntityName = EntityName.account.ToString()
' The query will retrieve all accounts whose address1_city is Sammamish.
query.Attributes = New String() {"address1_city"}
query.Values = New String() {"Sammamish"}
' Execute the retrieval.
Dim retrieved As BusinessEntityCollection = service.RetrieveMultiple(query)
See Also
Other Resources
© 2010 Microsoft Corporation. All rights reserved.

