zaterdag 27 april 2013

Extending the Microsoft Outlook Client for Microsoft Dynamics CRM 2011 (part 3)

Creating the Outlook Add-on

In my previous posts I have explained the general purpose of this feature and mentioned a webresource that will be included in Outlook.
In this example I will use Visual Studio 2010 and will create an Outlook 2010 Addon.
While I mention this, I must correct myself and choose an Outlook 2010 Add-in.

Usercontrol with webbrowser control
Once created we can start adding a usercontrol and give the related class a name like CRM Email Control.cs

Now we can add a webbrowser control on this usercontrol and give it a name like 'webBrowserCrm'
The url we can leave blank as it is.
Once we have added this user control, we can add a property Url to forward it to the webbrowser control like below:

public partial class CRM_Email_Control : UserControl
  public CRM_Email_Control()


  internal Uri Url
  {    set { webBrowserCrm.Url = value; }


  private void webBrowserCrm_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  {    webBrowserCrm.Visible = true;
  }   private void webBrowserCrm_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  {    webBrowserCrm.Visible = false;
  } } 

As you notice, I will hide the browser when loading and show the browser once finished loading.

Now we can edit the code in ThisAddin.cs.

At first we need to add the namespaces by adding:

using Microsoft.Office.Tools;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Win32;

We can replace the class like the code below. The commands explain everything.

public partial class ThisAddIn
 private const string webresourceName = "dh_Outlookemail.html";
  private CustomTaskPane dynamicHandsEmailPane;
  private Uri currentUrl;
  private string InstalledCrmUrl;
  private List<Explorer> explorers;

  private void ThisAddIn_Startup(object sender, System.EventArgs e)
    //get the crm url
    InstalledCrmUrl = Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\MSCRMClient", "WebAppUrl", null) as string;

    //when crm for outlook is not properly installed, we don't want to do anyting
    if (string.IsNullOrEmpty(InstalledCrmUrl))

    //add the outlook e-mail pane
    dynamicHandsEmailPane = this.CustomTaskPanes.Add(new CRM_Email_Control(), "DynamicHands E-mail");
    dynamicHandsEmailPane.Visible = false;
    dynamicHandsEmailPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop;
    dynamicHandsEmailPane.Height = 100; 
    //handle the selectionchanged of the current explorer
    Explorer exp = Application.ActiveExplorer();
    exp.SelectionChange += new ExplorerEvents_10_SelectionChangeEventHandler(exp_SelectionChange);

    //keep track of the explorer
    explorers = new List<Explorer>();


private void exp_SelectionChange()
//get the selection
  Selection selection = Application.ActiveExplorer().Selection;

   //handle the first selected item
  foreach (Object item in selection)
    if (item is MailItem)
     //generate an url based upon the tracked e-mail (or untracked e-mail)
      Uri url = GetUrl(item as MailItem);

      //prevent refresh and flickering
      if (currentUrl != url)
        //set url property and visibility

        dynamicHandsEmailPane.Visible = (url != null);

        //update the webcontrol url
        ((CRM_Email_Control)dynamicHandsEmailPane.Control).Url = url;

   //keep track of the current url
        currentUrl = url;

internal Uri GetUrl(MailItem item)
if (item == null)
    return null;

//check if mailitem contains a crmid property
  string crmId = (item.ItemProperties["crmid"] == null ? "" : item.ItemProperties["crmid"].Value);

//create an Uri based upon this id
  if (!string.IsNullOrEmpty(crmId))
   string crmUrl =
     string.Format( "{0}WebResources/{1}{2}",
       Uri.EscapeDataString(string.Format("?emailid={0}", crmId)));   

     return new Uri(crmUrl);

return null;

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
if (dynamicHandsEmailPane != null)
dynamicHandsEmailPane = null;

The installer will be available in the next post.

The sourcecode and related Dynamics CRM solution is available for download here:

Happy coding!


Geen opmerkingen: