Automated Deployment Tools for CRM 2011 – Import Solution with Progress

We use a lot of automated tools to deploy solutions to our test, UAT and production environments. One of the tools allows us to rapidly deploy a solution into multiple environments. For example, extracting a managed solution from DEV to TEST then to UAT, once UAT has passed we need to deploy up to 20 Dynamics CRM Online instances, doing this manually is too time consuming…

The tool looks at an xml file which specifies the connection settings for multiple CRM organizations, then when it’s run it’ll import and report the progress.

Here’s the code (stripped down to only include the import and progress reporting)

private static void ImportSolution(IOrganizationService sdk, string solutionPath)
{
    byte[] data = File.ReadAllBytes(solutionPath);
    Guid importId = Guid.NewGuid();
 
    ImportSolutionRequest request = new ImportSolutionRequest()
    {
        CustomizationFile = data,
        ImportJobId = importId
    };
 
    Thread t = new Thread(new ParameterizedThreadStart(ProgressReport));
    t.Start(importId);
 
    sdk.Execute(request);
    Console.WriteLine("imported");
}

First we have to create a new ImportSolutionRequest, the important thing to note here is that you need to specify an ImportJobId, this allows us to track the progress.

To report the progress

private static void ProgressReport(object importId)
{
    // connect to crm again, don't reuse the connection that's used to import
    IOrganizationService sdk = null;
    try
    {
        var job = sdk.Retrieve("importjob", (Guid)importId, new ColumnSet("solutionname", "progress"));
        decimal progress = Convert.ToDecimal(job["progress"]);
 
        Console.WriteLine("{0:N0}%", progress);
 
        if (progress == 100) { return; }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
 
    Thread.Sleep(2000);
    ProgressReport(importId);
}

The ProgressReport function queries the importjob entity with the importId we specified just before starting the solution import.

Enjoy!

2 Comments

Comments have been disabled for this content.