/// <summary>
        /// Downloads the solutions.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>List of solutions.</returns>
        public List <MSCRMSolution> DownloadSolutions(MSCRMConnection connection)
        {
            List <MSCRMSolution> solutionsLst = new List <MSCRMSolution>();

            _serviceProxy = cm.connect(connection);

            QueryExpression querySampleSolution = new QueryExpression
            {
                EntityName = "solution",
                ColumnSet  = new ColumnSet(true),
                Criteria   = new FilterExpression(),
            };

            querySampleSolution.Criteria.AddCondition("ismanaged", ConditionOperator.Equal, false);
            querySampleSolution.Criteria.AddCondition("isvisible", ConditionOperator.Equal, true);
            EntityCollection solutions = _serviceProxy.RetrieveMultiple(querySampleSolution);

            foreach (Entity solution in solutions.Entities)
            {
                EntityReference publisher      = (EntityReference)solution["publisherid"];
                string          description    = solution.Attributes.Contains("description") ? (string)solution["description"] : "";
                MSCRMSolution   MESCRMSolution = new MSCRMSolution
                {
                    UniqueName  = (string)solution["uniquename"],
                    DisplayName = (string)solution["friendlyname"],
                    Version     = (string)solution["version"],
                    Publisher   = publisher.Name,
                    Description = description
                };
                solutionsLst.Add(MESCRMSolution);
            }

            WriteSolutions(connection.ConnectionName, solutionsLst);

            return(solutionsLst);
        }
        /// <summary>
        /// Exports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        private void Export(MSCRMSolutionsTransportProfile profile)
        {
            try
            {
                //Set Data export folder
                if (!Directory.Exists(profile.SolutionExportFolder))
                {
                    Directory.CreateDirectory(profile.SolutionExportFolder);
                }

                MSCRMConnection connection = profile.getSourceConneciton();
                _serviceProxy = cm.connect(connection);

                //Download fresh list of solutions for versions update
                List <MSCRMSolution> solutions = DownloadSolutions(connection);

                DateTime now        = DateTime.Now;
                string   folderName = String.Format("{0:yyyyMMddHHmmss}", now);

                if (!Directory.Exists(profile.SolutionExportFolder + "\\" + folderName))
                {
                    Directory.CreateDirectory(profile.SolutionExportFolder + "\\" + folderName);
                }

                foreach (string SolutionName in profile.SelectedSolutionsNames)
                {
                    //Check if customizations are to be published
                    if (profile.PublishAllCustomizationsSource)
                    {
                        LogManager.WriteLog("Publishing all Customizations on " + connection.ConnectionName + " ...");
                        PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
                        _serviceProxy.Execute(publishRequest);
                    }
                    LogManager.WriteLog("Exporting Solution " + SolutionName + " from " + connection.ConnectionName);

                    ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest();
                    exportSolutionRequest.Managed      = profile.ExportAsManaged;
                    exportSolutionRequest.SolutionName = SolutionName;
                    exportSolutionRequest.ExportAutoNumberingSettings          = profile.ExportAutoNumberingSettings;
                    exportSolutionRequest.ExportCalendarSettings               = profile.ExportCalendarSettings;
                    exportSolutionRequest.ExportCustomizationSettings          = profile.ExportCustomizationSettings;
                    exportSolutionRequest.ExportEmailTrackingSettings          = profile.ExportEmailTrackingSettings;
                    exportSolutionRequest.ExportGeneralSettings                = profile.ExportGeneralSettings;
                    exportSolutionRequest.ExportIsvConfig                      = profile.ExportIsvConfig;
                    exportSolutionRequest.ExportMarketingSettings              = profile.ExportMarketingSettings;
                    exportSolutionRequest.ExportOutlookSynchronizationSettings = profile.ExportOutlookSynchronizationSettings;
                    exportSolutionRequest.ExportRelationshipRoles              = profile.ExportRelationshipRoles;

                    string managed = "";
                    if (profile.ExportAsManaged)
                    {
                        managed = "managed";
                    }
                    MSCRMSolution          selectedSolution        = solutions.Find(s => s.UniqueName == SolutionName);
                    string                 selectedSolutionVersion = selectedSolution.Version.Replace(".", "_");
                    ExportSolutionResponse exportSolutionResponse  = (ExportSolutionResponse)_serviceProxy.Execute(exportSolutionRequest);
                    byte[]                 exportXml = exportSolutionResponse.ExportSolutionFile;
                    string                 filename  = SolutionName + "_" + selectedSolutionVersion + "_" + managed + ".zip";
                    File.WriteAllBytes(profile.SolutionExportFolder + "\\" + folderName + "\\" + filename, exportXml);
                    LogManager.WriteLog("Export finished for Solution: " + SolutionName + ". Exported file: " + filename);
                }
                LogManager.WriteLog("Export finished for Profile: " + profile.ProfileName);
            }
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText);
                throw;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message);
                }
                else
                {
                    LogManager.WriteLog("Error:" + ex.Message);
                }
                throw;
            }
        }
        /// <summary>
        /// Downloads the solutions.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>List of solutions.</returns>
        public List<MSCRMSolution> DownloadSolutions(MSCRMConnection connection)
        {
            List<MSCRMSolution> solutionsLst = new List<MSCRMSolution>();
            _serviceProxy = cm.connect(connection);

            QueryExpression querySampleSolution = new QueryExpression
            {
                EntityName = "solution",
                ColumnSet = new ColumnSet(true),
                Criteria = new FilterExpression(),
            };
            querySampleSolution.Criteria.AddCondition("ismanaged", ConditionOperator.Equal, false);
            querySampleSolution.Criteria.AddCondition("isvisible", ConditionOperator.Equal, true);
            EntityCollection solutions = _serviceProxy.RetrieveMultiple(querySampleSolution);

            foreach (Entity solution in solutions.Entities)
            {
                EntityReference publisher = (EntityReference)solution["publisherid"];
                string description = solution.Attributes.Contains("description") ? (string)solution["description"] : "";
                MSCRMSolution MESCRMSolution = new MSCRMSolution
                {
                    UniqueName = (string)solution["uniquename"],
                    DisplayName = (string)solution["friendlyname"],
                    Version = (string)solution["version"],
                    Publisher = publisher.Name,
                    Description = description
                };
                solutionsLst.Add(MESCRMSolution);
            }

            WriteSolutions(connection.ConnectionName, solutionsLst);

            return solutionsLst;
        }