/// <summary> /// Method to export a solution from Dynamics CRM. /// </summary> /// <param name="outputDir">The path in which the exported solution file should be placed.</param> /// <param name="solutionUniqueName">The unique name of the solution to export.</param> /// <param name="managed">Should the solution being exported generate a managed or unmanaged solution file.</param> /// <returns>The file name as a string (not the path as that was an input parameter).</returns> public string ExportSolution(string outputDir, string solutionUniqueName, bool managed) { try { if (!string.IsNullOrEmpty(outputDir) && !outputDir.EndsWith(@"\", false, CultureInfo.CurrentCulture)) { outputDir += @"\"; } string ManagedStatus; if (managed) { ManagedStatus = "Managed"; } else { ManagedStatus = "UnManaged"; } ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest(); exportSolutionRequest.Managed = managed; exportSolutionRequest.SolutionName = solutionUniqueName; ExportSolutionResponse exportSolutionResponse; using (XrmService service = new XrmService(XRMConnectionString)) { exportSolutionResponse = (ExportSolutionResponse)service.Execute(exportSolutionRequest); } byte[] exportXml = exportSolutionResponse.ExportSolutionFile; string filename = solutionUniqueName + "_" + ManagedStatus + ".zip"; File.WriteAllBytes(outputDir + filename, exportXml); return(filename); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to roll back a solution. /// /// This is method is used where a backup copy of the solution is first exported as a possible restoration option before another file is imported. /// </summary> /// <param name="uniqueName">The unique name of the solution to roll back.</param> /// <param name="solutionFullPath">The path and file name of the solution file to be imported in order to restore the previous state of the solution.</param> public void RollbackSolution(string uniqueName, string solutionFullPath) { try { DeleteSolution(uniqueName); ImportSolution(solutionFullPath); using (XrmService service = new XrmService(XRMConnectionString)) { service.Execute(new PublishAllXmlRequest()); } } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to import a solution into Dynamics CRM. /// </summary> /// <param name="solutionFilePath">The path and file name of the file to import.</param> public void ImportSolution(string solutionFilePath) { try { byte[] fileBytes = File.ReadAllBytes(solutionFilePath); ImportSolutionRequest importSolutionRequest = new ImportSolutionRequest() { CustomizationFile = fileBytes }; using (XrmService service = new XrmService(XRMConnectionString)) { service.Execute(importSolutionRequest); } } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to add a component to a solution. /// </summary> /// <param name="solutionComponentType">The component type.</param> /// <param name="componentId">The component's identifier.</param> /// <param name="solutionUniqueName">The solution's unique name.</param> /// <returns>The result of the execution.</returns> public AddSolutionComponentResponse AddComponentToSolution(SolutionComponentType solutionComponentType, Guid componentId, string solutionUniqueName) { try { AddSolutionComponentRequest addSolutionComponentRequest = new AddSolutionComponentRequest() { ComponentType = (int)solutionComponentType, ComponentId = componentId, SolutionUniqueName = solutionUniqueName }; using (XrmService service = new XrmService(XRMConnectionString)) { return((AddSolutionComponentResponse)service.Execute(addSolutionComponentRequest)); } } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }