Export (0) Print
Expand All

Using the QueryByAttribute Class

banner art

[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.

PropertyDescription
EntityNameSpecifies which kind of entity will be retrieved. A query expression can only retrieve a collection of one entity type.
ColumnSetSpecifies the set of attributes (columns) to retrieve.
AttributesSpecifies the set of attributes selected in the query.
ValuesSpecifies the attribute values to look for when the query is executed.
OrdersSpecifies the order in which the entity instances are returned from the query.
PageInfoSpecifies 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.


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft