Export (0) Print
Expand All

Using QueryExpression

CRM 1.2
banner art

QueryExpression is the class you use to build complex queries for use with the RetrieveMultiple method or the RetrieveMultiple message.

Example

[C#]
// Set up the CRM Service.
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Retrieve all accounts user has read access to
// Create the query expression.
QueryExpression query1 = new QueryExpression();

// Set the query to retrieve accounts.
query1.EntityName = EntityName.account.ToString();

// Create the request object.
RetrieveMultipleRequest retrieve1 = new RetrieveMultipleRequest();

// Set the properties of the request object.
retrieve1.Query = query1;

// Execute the request.
RetrieveMultipleResponse retrieved1 = (RetrieveMultipleResponse) service.Execute(retrieve1);

// Return Accounts whose owner's last name is not Cannon
// Create a column set that holds the names of the columns to be retrieved.
ColumnSet cols = new ColumnSet();

// Set the properties of the column set.
cols.Attributes = new string [] {"name", "accountid"};

// Create the condition expression.
ConditionExpression condition = new ConditionExpression();

// Set the condition for the retrieval to be when the last name of the account's owner is not Cannon.
condition.AttributeName = "lastname";
condition.Operator = ConditionOperator.NotEqual;
condition.Values = new string [] {"Cannon"};

// Build the filter based on the condition.
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {condition};

// Create a LinkEntity to link the owner's information to the account.
LinkEntity link = new LinkEntity();

// Set the properties of the LinkEntity.
link.LinkCriteria = filter;

// Set the linking entity to be the account.
link.LinkFromEntityName = EntityName.account.ToString();

// Set the linking attribute to be the owninguser.
link.LinkFromAttributeName = "owninguser";

// Set the attribute being linked to to be the systemuserid.
link.LinkToAttributeName = "systemuserid";

// Set the entity being linked to to be the systemuser.
link.LinkToEntityName = EntityName.systemuser.ToString();

// Create the query.
QueryExpression query = new QueryExpression();

// Set the properties of the query.
query.EntityName = EntityName.account.ToString();
query.ColumnSet = cols;
query.LinkEntities = new LinkEntity[] {link};

// Create the request object.
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

// Set the properties of the request object.
retrieve.Query = query;

// Execute the request.
RetrieveMultipleResponse retrieved2 = (RetrieveMultipleResponse) service.Execute(retrieve);

[Visual Basic .NET]
' Set up the CRM Service.
Dim service As New CrmService()
service.Credentials = System.Net.CredentialCache.DefaultCredentials

' Retrieve all accounts user has read access to
' Create the query expression.
Dim query1 As New QueryExpression()

' Set the query to retrieve accounts.
query1.EntityName = EntityName.account.ToString()

' Create the request object.
Dim retrieve1 As New RetrieveMultipleRequest()

' Set the properties of the request object.
retrieve1.Query = query1

' Execute the request.
Dim retrieved1 As RetrieveMultipleResponse = CType(service.Execute(retrieve1), RetrieveMultipleResponse)
'-------------------------------------------

' Return accounts whose owner's last name is not Cannon
' Create a column set that holds the names of the columns to be retrieved.
Dim cols As New ColumnSet()

' Set the properties of the column set.
cols.Attributes = New String() {"name", "accountid"}

' Create the condition expression.
Dim condition As New ConditionExpression()

' Set the condition for the retrieval to be when the last name of the account's owner is not Cannon.
condition.AttributeName = "lastname"
condition.Operator = ConditionOperator.NotEqual
condition.Values = New String() {"Cannon"}

' Build the filter based on the condition.
Dim filter As New FilterExpression()
filter.FilterOperator = LogicalOperator.And
filter.Conditions = New ConditionExpression() {condition}

' Create a LinkEntity to link the owner's information to the account.
Dim link As New LinkEntity()

' Set the properties of the LinkEntity.
link.LinkCriteria = filter

' Set the linking entity to be the account.
link.LinkFromEntityName = EntityName.account.ToString()

' Set the linking attribute to be the owninguser.
link.LinkFromAttributeName = "owninguser"

' Set the attribute being linked to to be the systemuserid.
link.LinkToAttributeName = "systemuserid"

' Set the entity being linked to to be the systemuser.
link.LinkToEntityName = EntityName.systemuser.ToString()

' Create the query.
Dim query As New QueryExpression()

' Set the properties of the query.
query.EntityName = EntityName.account.ToString()
query.ColumnSet = cols
query.LinkEntities = New LinkEntity() {link}

' Create the request object.
Dim retrieve As New RetrieveMultipleRequest()

' Set the properties of the request object.
retrieve.Query = query

' Execute the request.
Dim retrieved2 As RetrieveMultipleResponse = CType(service.Execute(retrieve), RetrieveMultipleResponse)
'----------------------------------------------

© 2007 Microsoft Corporation. All rights reserved.


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