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()
  {

   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

zaterdag 20 april 2013

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

The Microsoft Dynamics CRM Outlook client
By default the Microsoft Dynamics CRM outlook client provides users the ability to track e-mails by using the "Track" button. All users are familiar with this functionality and use this whenever an e-mail is required to be tracked by your organization.

What actually is missing here is a basic feature to register more information related to this e-mail and customer. For example, a user tracks an e-mail message and wants to fill in related information and create a follow-up task as well. It can be done when a user clicks the "View in CRM" button but it may take many clicks and open windows to actually take care of this e-mail.

Extending the client
There are many ways to extend the Outlook client but for this example I will allow the user to simply click on a follow-up button that triggers a dialog process from within a managed html webresource


Creating a dialog
Because this blog is not about creating dialog processes I will ask you to go to the following video and make sure you create a dialog process based upon the e-mail entity and you know the corresponding guid. You can get the guid by running the dialog from within Microsoft CRM and press ctrl+n. You will probably see a long url in Internet Explorer. The id is marked in bold.
https://myorg.dynamics.com/cs/dialog/rundialog.aspx/cs/dialog/rundialog.aspx?DialogId=%7bE49AC7F1-BFF5-44DF-BE2C-2E05A7946B92%7d&amp;EntityName=email&amp;ObjectId={ECDB724E-EDA9-E211-B63A-D4856451BCD1}

Creating a html webresource
As for the html webresource you can use the following code. Make sure you enter the dialog guid correctly and notice this webresouce requires a parameter "emailid" in order to run.
Before actually start embedding this webresource in Outlook, make sure it will run in your browser. Let's name this webresource dh_Outlookemail.html and test this webresource by replacing the bold guid by a correct e-mail guid and entering the following url:

https://myorg.dynamics.com/WebResources/dh_Outlookemail.html%3Femailid%3D%7b32522B09-D3A9-E211-B554-3C4A92DBC855%7d


 

<html><head><meta charset="utf-8">
<script language="javascript"> 

var emailId = null;
var dialogId = 'E49AC7F1-BFF5-44DF-BE2C-2E05A7946B92';

getParams = functionundefined){
   var href = decodeURIComponentundefined location.href );
    var vars = [], hash;
    var hashes = href.sliceundefinedhref.indexOfundefined'?') + 1).splitundefined'&');
    for undefinedvar i = 0; i < hashes.length; i++) {
        hash = hashes[i].splitundefined'=');
        vars.pushundefinedhash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

ShowRelatedData = functionundefined) {
  var parameters = getParamsundefined);
  ifundefined parameters['emailid'] != null ) 
  {
      document.getElementByIdundefined 'btnStartFollupupDialog').style.display='block';
      emailId = parameters['emailid']; 
  }
}

showFollowupDialog = functionundefined) {
    var url = location.href.substringundefined0,location.href.length - location.pathname.length) + '/cs/dialog/rundialog.aspx';
    url += "/cs/dialog/rundialog.aspx?DialogId=%7b" + dialogId + "%7d&EntityName=email&ObjectId=" + emailId ;
    window.openundefinedurl, "", "status=no,scrollbars=no,toolbars=no,menubar=no,location=no");
}

</script>
</head>
<body bgcolor="#dddddd" onload="ShowRelatedDataundefined);">
  <button id="btnStartFollupupDialog" onclick="showFollowupDialogundefined);">Run follup up dialog</button><br>
</body></html>




What's next?
Part 3 (the actual Microsoft Outlook Client for Dynamics CRM 2011 Extension) and sourcecode will be available soon.

Greetz.
Boudewijn,

vrijdag 19 april 2013

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

How do you do?
It has been a while since I've logged in, nevertheless this blog should be worthwhile whenever the Microsoft Dynamics CRM Outlook Client does not completely fulfill your client's needs.

What is this about?
Well as far as the Microsoft Outlook Client exists, users can track their e-mail messages and tasks into CRM. The only thing some users are missing is a way to view more related information or functionality to actually follow up this e-mail or task with a simple click from within the Outlook Client.

Compatibility. What about it?
Well.. as long as users will adopt the Crm Outlook Client for Microsoft Dynamics CRM 2011 in Microsoft Outlook 2010, this add-on will be suitable. However there are ways to extent previous versions of this Outlook client as well.
Because this add-on uses a default webresource in Microsoft Dynamics CRM this can be used for on-premise or online. It does not matter at all.

How does this work?
Well, basically it is just an idea. The only reference that will be used in this blog is Walkthrough: Creating Your First Application-Level Add-in for Outlook. Furthermore, knowledge of creating a web resource in Microsoft Dynamics CRM will be very useful and required as well.

More information about the how to in the next blog.

Greetz, Boudewijn.