Saturday 21 July 2012

Import solution programmatically in Microsoft Dynamics CRM 2011 (ImportSolutionRequest)

I've been working in trying to automate as much of our build as possible and one of the first things to try to automate was importing solutions.

The ImportSolutions method is designed to be invoked from a WPF application. SolutionsDirectory is entered through the GUI and should only contain solution files.

Not shown is the PublishSolution method, which is, hopefully, fairly self explanatory and only tries to publish the solution if the import was successful.

 private bool ImportSolutions(OrganizationServiceProxy OrgService, string SolutionsDirectory)
 {
    ImportSolutionRequest SolutionRequest;
    byte[] Solution;
 
    string[] files = Directory.GetFiles(SolutionsDirectory, Constants.ZipExtension);
 
    foreach (string file in files)
    {
        Solution = File.ReadAllBytes(file);
 
        SolutionRequest = new ImportSolutionRequest();
        SolutionRequest.CustomizationFile = Solution;
        SolutionRequest.ImportJobId = Guid.NewGuid();
        SolutionRequest.ConvertToManaged = true; //does this actually work?
 
        try
        {
            OrgService.Execute(SolutionRequest);
 
            Entity ImportJob = new Entity("importjob");
 
            ImportJob = OrgService.Retrieve(ImportJob.LogicalName, SolutionRequest.ImportJobId, new ColumnSet(true));
 
            XDocument xdoc = XDocument.Parse(ImportJob["data"].ToString());
 
            String ImportedSolutionName = xdoc.Descendants("solutionManifest").Descendants("UniqueName").First().Value;
            bool SolutionImportResult = xdoc.Descendants("solutionManifest").Descendants("result").First().FirstAttribute.Value
                == "success" ? true : false;
 
            Guid? SolutionId = GetSolutionGuid(OrgService, ImportedSolutionName);
 
            if (SolutionImportResult && SolutionId != null)
            {
                PublishSolution(OrgService, file, (Guid)SolutionId);
            }
 
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("An error occurred while uploading solution {0}.{1}Exception:{2}", file, Environment.NewLine, ex));
 
            return false;
        }
 
    }
 
    return true;
 }

3 comments:

  1. What message did you send to publish the solution? I can't see how to publish a single solution, only all solutions (PublishAllXmlRequest) or particular resources (PublishXmlRequest) which doesn't accept a solution. Thanks.

    ReplyDelete
  2. I can't find the code for the PublishSolution method. Two options:

    A misunderstanding on my part of what PublishAllXmlRequest did
    It actually published the components using PublishXmlRequest, creating the relevant ParameterXml from the solution file.

    It was removed from later code, which i do have, so I'd say the former is more likely even if the method signature makes little sense.

    ReplyDelete
  3. Is there a tool that export and import a solution ?

    ReplyDelete