Design part

You should leave an empty space of 22 pixels at the bottom of the form

image

Code behind part

New variables

Adds two new variables on the form, one for the ConnectionManager, one for the connection statusbar 

/// <summary>
/// Connection control
/// </summary>
CrmConnectionStatusBar ccsb;

/// <summary>
/// Connection manager
/// </summary>
ConnectionManager cManager;

 

Instanciate the connection manager

On the constructor of the form, instantiate the class ConnectionManager. The only argument of the constructor is the form itself

this.cManager = new ConnectionManager(this);

 

Implement ConnectionManager events

ConnectionSucceed

This event is triggered when the connection controls found the server, the organization and validated user credentials

this.cManager.ConnectionSucceed += new ConnectionManager.ConnectionSucceedEventHandler(cManager_ConnectionSucceed);

ConnectionFailed

This event is triggered when something goes wrong when trying to connect to a CRM server (server not found, organization not found, authentication failure, etc)

this.cManager.ConnectionFailed += new ConnectionManager.ConnectionFailedEventHandler(cManager_ConnectionFailed);

StepChanged

This event is triggered when a connection step changed (connecting to service, creating OrganizationService proxy, testing the credentials, etc.)

this.cManager.StepChanged += new ConnectionManager.StepChangedEventHandler(cManager_StepChanged);

 

Instanciate the Connection Statusbar

The statusbar constructor takes the ConnectionManager instance as the only parameter

// Instantiate and add the connection control to the form
ccsb = new CrmConnectionStatusBar(this.cManager);
this.Controls.Add(ccsb);

 

What your the constructor of you form should looks like

public Form1()
{
    InitializeComponent();

    // Create the connection manager with its events
    this.cManager = new ConnectionManager(this);
    this.cManager.ConnectionSucceed += new ConnectionManager.ConnectionSucceedEventHandler(cManager_ConnectionSucceed);
    this.cManager.ConnectionFailed += new ConnectionManager.ConnectionFailedEventHandler(cManager_ConnectionFailed);
    this.cManager.StepChanged += new ConnectionManager.StepChangedEventHandler(cManager_StepChanged);

    // Instantiate and add the connection control to the form
    ccsb = new CrmConnectionStatusBar(this.cManager);
    this.Controls.Add(ccsb);
}

Create methods for the connection manager event

 

/// <summary>
/// Occurs when the connection manager sends a step change
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cManager_StepChanged(object sender, StepChangedEventArgs e)
{
    this.ccsb.SetMessage(e.CurrentStep);
}

/// <summary>
/// Occurs when the connection to a server failed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cManager_ConnectionFailed(object sender, ConnectionFailedEventArgs e)
{
    // Set error message
    this.ccsb.SetMessage("Error: " + e.FailureReason);
    
    // Clear the current organization service
    this.service = null;
}

/// <summary>
/// Occurs when the connection to a server succeed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cManager_ConnectionSucceed(object sender, ConnectionSucceedEventArgs e)
{
    // Store connection Organization Service
    this.service = e.OrganizationService;
    
    // Displays connection status
    this.ccsb.SetConnectionStatus(true, e.ConnectionDetail);

    // Clear the current action message
    this.ccsb.SetMessage(string.Empty);

    // Do action if needed
    if (e.Parameter != null)
    {
        if (e.Parameter.ToString() == "WhoAmI")
        {
            WhoAmI();
        }
    }
}

Last edited Jan 21, 2012 at 3:56 PM by tanguy92, version 10

Comments

No comments yet.