Windows Forms Hosted Control in USD

1 Comment

In this topic, we will go through how to integrate Unified Service Desk with a Windows Forms UII Hosted Application. For example, you may want to build from scratch a windows forms application that your agents use, that is integrated with USD. Using the USD Developer Guide, we are able to create applications that run as hosted controls and contain code that talks with USD. This is different from the scenario where we do not have source code for an application that we are integrating with USD, where we would create a UII adapter. In this case, we create the UII WIndows Forms hosted control from scratch.

The new Windows Forms UII application may hold information such as the Account Name, which then looks up other information in a 3rd party system. Consider the scenario where we want to:

  1. Attach the application to Unified Service Desk
  2. Populate the application’s text box with the name of the account a user is searching from in USD
  3. If the user updates the account from the Windows application, we want to tell USD that an update has been made, therefore update the context in USD so we can deal with changes such as updating other information

To add a Windows Forms hosted control, go through the following steps.

In Visual Studio, create a Windows Forms Hosted Control:

This creates the following files:

Expand the references. You may be missing references:

If so, browse out to the USD directory located at C:\Program Files\Microsoft Dynamics CRM USD\USD and select any of the missing references above.

Open the UiiWinformControl.cs. This is a windows form:

Add a label Name and a textbox txtName. Add a button Update Context. We will use this button to update the context in USD.

Add the following code to the button:

// Get the current context and create a new context object from it.
string temp = Context.GetContext();
Context updatedContext = new Context(temp);

We will update the context in USD with the value of the Name textbox.

updatedContext["name"] = txtName.Text;

Lastly, we need to tell USD that the context has been updated. We do that with this command:

   // Notify Unified Service Desk of this new context information
   FireChangeContext(new ContextEventArgs(updatedContext));

The code should now look like this:

We will also update our text box with the value of the context name from the account when it loads:

public override void NotifyContextChange(Context context)
{
    txtName.Text = context["name"];
 
    base.NotifyContextChange(context);
}

Now we need to deploy the custom hosted control to USD.

Copy the DLL from the Visual Studio project to the USD client folder:

In USD Configuration, create a new Hosted Control with the settings below. Add the assembly information at the bottom:

We will now need to launch the application in USD. We can add an action to do this so when an account is loaded our hosted control will also load.

Create an action to open the hosted control:

Add it to the BrowerDocumentComplete of the account:

Now when an account opens, we have a new tab showing the Sample Windows Hosted Control. The Name has been populated by the context name variable:

Now, update the name and press Update Context to update the context:

We can see the Context name variable has received the update:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Windows Forms Hosted Control in USD

  1. Hi Carl,
    Thanks for sharing the article. It is really helpful.

    I’ve a query here. Is it possible to get the USD context on the initialisation of the custom hosted control?

    Thanks,
    Sumit

Leave a Reply

Your email address will not be published. Required fields are marked *