The sample code below shows how you can search for a contact based on the contacts firstname, lastname and company name.
You’ll need to link the contact and company entity.

// Create a column set holding the names of the columns to be retrieved.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string[] { "contactid", "fullname" };</code>

// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "name";
condition.Operator = ConditionOperator.Like;
condition.Values = new string[] { Company + "%" };

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

LinkEntity link = new LinkEntity();

// Set the LinkEntity properties.
link.LinkCriteria = filter;
link.LinkFromEntityName = EntityName.contact.ToString();
link.LinkFromAttributeName = "parentcustomerid";
link.LinkToAttributeName = "accountid";
link.LinkToEntityName = EntityName.account.ToString();

ConditionExpression conditionFN = new ConditionExpression();
conditionFN.AttributeName = "firstname";
conditionFN.Operator = ConditionOperator.Like;
conditionFN.Values = new string[] { Firstname + "%" };

ConditionExpression conditionLN = new ConditionExpression();
conditionLN.AttributeName = "lastname";
conditionLN.Operator = ConditionOperator.Like;
conditionLN.Values = new string[] { Lastname + "%" };

FilterExpression cfilter = new FilterExpression();
cfilter.FilterOperator = LogicalOperator.And;
cfilter.Conditions = new ConditionExpression[] { conditionFN, conditionLN };

// Create an instance of the query expression class.
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.contact.ToString();
query.ColumnSet = cols;
query.Criteria = cfilter;
query.LinkEntities = new LinkEntity[] { link };

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

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

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

Advertisements