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.

1

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.

 2

 

cheers
– jonw

Comments (2)

  1. Eric Johnston says:

    Very nice!  XRMagic Search does something similar for Dynamics CRM 2011. The add-on allows users to search across 15 entities and 40 fields out-of-box and can be configured to search any number of custom entities and attributes. We're charging a little bit for it because it's packaged, supported, and ready to go as a complete solution. But people who know JScript, ODATA, and XML can roll their own multi-entity search page for CRM 2011 in 10-20 hours. We packaged XRMagic Search for those who want this functionality without building it themselves and don't want to spend a fortune for an add-on like this. We even provide a free version. Some of the tools out there cost more than CRM does each month!  That's crazy.  Thanks, EJ

  2. jalal says:

    Hi,

        nice code..

        i am new to mscrm,can pls share this code to me.

    Thanks,

    Jalal