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.
public partial class CRM_Email_Control : UserControl
{
public CRM_Email_Control()
{
InitializeComponent();
}
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.
ThisAddin
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))
return;
//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>();
explorers.Add(exp);
}
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;
}
}
break;
}
}
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}",
InstalledCrmUrl,
webresourceName,
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)
{
Globals.ThisAddIn.CustomTaskPanes.Remove(dynamicHandsEmailPane);
}
dynamicHandsEmailPane = null;
}
Installer
The installer will be available in the next post.
The sourcecode and related Dynamics CRM solution is available for download here:
http://sdrv.ms/13C0bSK
Happy coding!
Geetz.
Boudewijn