Cross Entity Search Application
Searching across entities is a great feature. I built a web application that integrates with CRM Online to do this. You can enter a phone number, a last name, an account name , or opportunity topic and the application searches for the results. It will give you a list of return items. The edit button will open the record.
The web application is pretty easy to build. It’s a single page with a grid view that loads a data set into the grid. There’s plenty of articles on how to do authentication to CRM Online, so I’ll just include the code to query with. Just repeat for each entity you want to search.
public bool GetContacts(string sValue)
{
// notetext subject modifiedon
bool rc = false;
try
{
// Create the ConditionExpression.
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "lastname";
condition1.Operator = ConditionOperator.Like;
condition1.Values = new object[] { "%" + sValue + "%" };
// Create the ConditionExpression.
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = "telephone1";
condition2.Operator = ConditionOperator.Like;
condition2.Values = new object[] { "%" + sValue + "%" };
// Build the filter based on the condition.
// Create the "created in last 30 days" filter
FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] { condition1, condition2 };
filter.FilterOperator = LogicalOperator.Or;
// Create the query.
QueryExpression query = new QueryExpression();
query.Criteria = filter;
// Set the properties of the query.
query.EntityName = "contact";
ColumnSet Cols = new ColumnSet();
Cols.Attributes = new string[] { "lastname", "contactid","telephone1"};
query.ColumnSet = Cols;
// Create the request object.
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
// Set the properties of the request object.
retrieve.Query = query;
retrieve.ReturnDynamicEntities = false;
// Execute the request.
RetrieveMultipleResponse retrieved =
(RetrieveMultipleResponse)CrmLib.GetService().Execute(retrieve);
if ((retrieved != null) && (retrieved.BusinessEntityCollection.BusinessEntities.Length > 0))
{
ContactList = retrieved.BusinessEntityCollection;
if (ContactList.BusinessEntities.Length > 0)
{
// Add all the Lead records to a data table
CrmService.contact aEntity = null;
for (int x = 0; x < ContactList.BusinessEntities.Length; x++)
{
aEntity = (CrmService.contact)ContactList.BusinessEntities[x];
DataRow aRow = null;
if (aEntity != null)
{
string link = "https://" + _organization + ".crm.dynamics.com/sfa/conts/edit.aspx?id={" +tity.contactid.Value + "}#";
aRow = CrmLib.CRMRecordsDT.Rows.Add(aEntity.contactid.Value, "contact", aEntity.telephone1,aEntity.lastname, link);
}
}
rc = true;
}
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Detail.InnerXml);
Login();
return rc;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return rc;
}
return rc;
}
A couple of thing to notice.
I’m adding records to a the data set CrmLib.CRMRecordsDT. The record contains the data shown in the grid. The grid has an edit button that has it’s onclick function set to open the link. The link is created based on the record id and stored in the dataset. To search other records, change the query and columns to return and insert into the dataset for the grid to load.
cheers
- jonw
Comments
- Anonymous
March 14, 2011
The comment has been removed - Anonymous
May 02, 2013
Hi, nice code.. i am new to mscrm,can pls share this code to me. Thanks, Jalal