Customer Care Accelerator (CCA) for CRM 2011 (Part 3 – Hosting Bing as Hosted Control)

Please refer the earlier post

1. Customer Care Accelerator – Part 1

2. Customer Care Accelerator – Part 2

Here we would be hosting the Bing search page using Hosted Control.

Add a new class library project to the Agent Desktop Solution.

Add a new User Control class to the project

Add references to the following assemblies

Inherit the HostedControl class

And override the implement the Hosted Control class in the following manner

</pre>
using System;
using System.Windows.Forms;
using Microsoft.Uii.Common.Logging;
using Microsoft.Uii.Csr;
using Microsoft.Uii.Csr.Browser.Web;

namespace MyHostedControl
{
 /// <summary>
 /// Class BingHostedControl
 /// </summary>
 public partial class BingHostedControl : HostedControl
 {
 /// <summary>
 /// The navigation URL
 /// </summary>
 private readonly string _navigationUrl;

/// <summary>
 /// Represents the web page dom
 /// </summary>
 private HtmlDocument _doc;

/// <summary>
 /// The web browser
 /// </summary>
 private WebBrowserExtended _webBrowser;

/// <summary>
 /// Initializes a new instance of the <see cref="BingHostedControl"/> class.
 /// </summary>
 public BingHostedControl()
 {
 InitializeComponent();
 }


 /// <summary>
 /// Initializes a new instance of the <see cref="BingHostedControl"/> class.
 /// </summary>
 /// <param name="appId">The app id.</param>
 /// <param name="appName">Name of the app.</param>
 /// <param name="extensionXml">The extension XML.</param>
 public BingHostedControl(Guid appId, string appName, string extensionXml)
 : base(appId, appName, extensionXml)
 {
 _navigationUrl = "http://www.bing.com";
 InitializeComponent();
 }

/// <summary>
 /// Initializes this instance.
 /// </summary>
 public override void Initialize()
 {
 try
 {
 InitializeBrowser();
 base.Initialize();
 }
 catch (Exception exception)
 {
 Logging.Error("Bing Hosted App", "Error occurred initializing the Bing control", exception);
 throw;
 }
 }

/// <summary>
 /// Initialize Browser
 /// </summary>
 private void InitializeBrowser()
 {
 _webBrowser = new WebBrowserExtended(false, true, true);
 _webBrowser.Dock = DockStyle.Fill;
 _webBrowser.Visible = true;
 Controls.Add(_webBrowser);
 }


 /// <summary>
 /// Close the web browser process before closing the hosted control
 /// </summary>
 public override void Close()
 {
 try
 {
 if (_webBrowser != null)
 {
 _webBrowser.CloseBrowser();
 _webBrowser.Dispose();
 }

base.Close();
 }
 catch (Exception exception)
 {
 Logging.Error("Bing Hosted App", "Error occurred initializing the Bing control", exception);
 throw;
 }
 }


 /// <summary>
 /// Method is used to perform automation
 /// </summary>
 /// <param name="args">event arguments</param>
 protected override void DoAction(RequestActionEventArgs args)
 {
 try
 {
 if (args == null)
 {
 return;
 }

if (args.Action.Equals("default"))
 {
 _webBrowser.DocumentComplete += WebBrowser_DocumentComplete;
 _webBrowser.Navigate(_navigationUrl);
 }

base.DoAction(args);
 }
 catch (Exception exception)
 {
 Logging.Error("Bing Hosted App", "Error occurred initializing the Bing control", exception);
 throw;
 }
 }


 /// <summary>
 /// Called when web application is completely loaded
 /// </summary>
 /// <param name="sender">sender object</param>
 /// <param name="e">event arguments</param>
 private void WebBrowser_DocumentComplete(object sender, DocumentCompleteEventArgs e)
 {
 try
 {
 if (_webBrowser != null &&
 (_webBrowser.ReadyState == WebBrowserReadyState.Complete ||
 _webBrowser.ReadyState == WebBrowserReadyState.Loaded))
 {
 _doc = _webBrowser.Document as HtmlDocument;
 if (_doc != null)
 {
 // perform DOM operation as reuquired
 //_document = _doc.GetElementsByTagName("FRAME");
 }
 }
 }
 catch (InvalidOperationException exception)
 {
 Close();
 Logging.Error("Bing Hosted App", "Error occurred while loading Bing control", exception);
 throw;
 }
 catch (Exception exception)
 {
 Close();
 Logging.Error("Bing Hosted App", "Error occurred while loading Bing control", exception);
 throw;
 }
 }
 }
}
<pre>

Create a new UII Hosted Application record in CRM

Here we will see a default UII Actions defined for the Hosted Control which we can access in the DoAction method of the Hosted Control.

Run the Agent Desktop.exe

We can see the Bing search page hosted inside the Agent Desktop as a Hosted Control. We will get into details of the Hosted Control and other stuffs and will see more real world examples in the future posts.

One thought on “Customer Care Accelerator (CCA) for CRM 2011 (Part 3 – Hosting Bing as Hosted Control)

Leave a comment