Pieter van der Westhuizen

How to create an Internet Explorer Bar for Microsoft Dynamics CRM

Back in July 2010 I showed how using Add-in Express 2010, you can easily build additional integrations between Microsoft Outlook and Microsoft Dynamics CRM. In today’s post I’ll show you how to write another integration to Dynamics CRM, only this time it is using Add-in Express for Internet Explorer and .net.

We will write an add-on for Internet Explorer which will show the selected contact in Dynamics CRM’s orders on an IE bar.

Start by creating a new Add-in Express IE Add-on in Visual Studio.

Creating a new ADX IE Add-on in Visual Studio

C# will be our language of choice and the add-in will be version-neutral.

Selecting C# as the programming language of choice

Once the wizard has completed, add a new IE Bar to your project. You will find it under Add-in Express Items > Internet Explorer

Adding a new IE Bar to the project

Design the Explorer bar. I’ve added some branding and a list view which will display the customers’ orders.

Designing the newly created Explorer bar

Next, switch back to the IEModule designer surface and click on the ellipses(…) button in the Bars property.

Setting the bar's properties

Set the BarType property to the name of the IE Bar you’ve added previously and the MenuText and Title property to Add-in Express Dynamics CRM Order View. Finally set the LoadAtStartup property to True. Setting the AddToolButton property to True will add a menu entry for your add-in to Internet Explorers’ command bar.

We will need to respond to Internet Explorer events; in order to accomplish this add a new ADXIEHTMLDocEvents component to the IEModule designer surface and set its SupportedEvents property to TableRow. Next, generate an event handler for the OnPropertyChange event by double clicking next to its name in the events list of the property grid.

Generating an event handler for the OnPropertyChange event

If you look at the page source of the Contacts view of Dynamics CRM, you’ll notice that the selected contacts TableRow(TR) element has a class name of ms-crm-list-SelectedRow. In the following code we will check whether the TableRow element that changed actually has that class name, and if it does we will grab the selected contacts’ id and load their associated account’s orders in the listview.

private void adxiehtmlDocEvents1_OnPropertyChange(object sender, object eventObject)
{
    if (eventObject is IHTMLEventObj2)
    {
        IHTMLEventObj2 eventObj = (IHTMLEventObj2)eventObject;
        if (eventObj.srcElement is IHTMLElement)
        {
            IHTMLElement eventElement = (IHTMLElement)eventObj.srcElement;
            if (eventElement.className == "ms-crm-List-SelectedRow")
            {
                string id = eventElement.getAttribute("oid").ToString();
 
                CrmAuthenticationToken token = new CrmAuthenticationToken();
                token.AuthenticationType = 0;
                token.OrganizationName = "NorthwindTraders";
 
                using (CrmService service = new CrmService())
                {
                    service.Url =
						"http://crmserver:5555/mscrmservices/2007/crmservice.asmx";
                    service.CrmAuthenticationTokenValue = token;
                    service.Credentials = new NetworkCredential(
						"Administrator", "MyPassord", "CRMSERVER");
 
                    contact crmContact = (contact)service.Retrieve(
						EntityName.contact.ToString(), new Guid(id), new AllColumns());
 
                    Customer cust = crmContact.parentcustomerid;
 
                    ConditionExpression condition = new ConditionExpression();
                    condition.AttributeName = "customerid";
                    condition.Operator = ConditionOperator.Like;
                    condition.Values = new string[] { cust.Value.ToString() };
 
                    FilterExpression filter = new FilterExpression();
                    filter.FilterOperator = LogicalOperator.And;
                    filter.Conditions.Add(condition);
 
                    QueryExpression query = new QueryExpression();
                    query.EntityName = EntityName.salesorder.ToString();
                    query.ColumnSet = new AllColumns();
                    query.Criteria = filter;
 
                    BusinessEntityCollection orders = service.RetrieveMultiple(query);
 
                    ((CrmBar)this.Bars[0].BarObj).lblNumberOfOrders.Text =
						String.Format("{0} has {1} orders.", 
							cust.name, orders.BusinessEntities.Count);
                    ((CrmBar)this.Bars[0].BarObj).lvOrders.Items.Clear();
                    foreach (salesorder order in orders.BusinessEntities)
                    {
                        ListViewItem lvItemOrderNum =
							new ListViewItem(order.ordernumber);
                        ListViewItem.ListViewSubItem lvItemTotal =
							new ListViewItem.ListViewSubItem(
								lvItemOrderNum, order.totalamount.Value.ToString("c"));
                        ListViewItem.ListViewSubItem lvItemName =
							new ListViewItem.ListViewSubItem(
								lvItemOrderNum, order.name);
                        lvItemOrderNum.SubItems.Add(lvItemTotal);
                        lvItemOrderNum.SubItems.Add(lvItemName);
                        ((CrmBar)this.Bars[0].BarObj).lvOrders.Items.Add(lvItemOrderNum);
                    }
                }
            }
        }
    }
}

If all went well, you should end up with an Internet Explorer Add-on for Dynamics CRM that looks like this:

Custom Internet Explorer Add-on for Dynamics CRM

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample add-on was developed using Add-in Express for Internet Explorer and .net:
C# sample IE add-on

Post a comment

Have any questions? Ask us right now!

string(10) "SUMMERSALE"