/// <summary>
        /// Imports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        private void Import(MSCRMSolutionsTransportProfile profile)
        {
            //Check if there is a solutions to import
            if (Directory.Exists(profile.SolutionExportFolder))
            {
                IOrderedEnumerable <string> subDirectories = Directory.GetDirectories(profile.SolutionExportFolder).OrderByDescending(x => x);
                if (subDirectories.Count <string>() == 0)
                {
                    LogManager.WriteLog("There are no solutions for import.");
                    return;
                }

                //Check which solutions to import: Newest, Oldest, specific exprot date solutions
                string solutionsToImportFolder = "";
                if (profile.SolutionsToImport == "Newest")
                {
                    solutionsToImportFolder = subDirectories.ElementAt(0);
                }
                else if (profile.SolutionsToImport == "Oldest")
                {
                    solutionsToImportFolder = subDirectories.ElementAt(subDirectories.Count <string>() - 1);
                }
                else
                {
                    solutionsToImportFolder = subDirectories.First(s => s.EndsWith(profile.SolutionsToImport));
                }

                if (solutionsToImportFolder == "")
                {
                    LogManager.WriteLog("The specified solutions to import were not found.");
                    return;
                }

                //get all solutions archives
                string[] solutionsArchivesNames = Directory.GetFiles(solutionsToImportFolder, "*.zip");
                if (solutionsArchivesNames.Count <string>() == 0)
                {
                    LogManager.WriteLog("There are no solutions for import.");
                    return;
                }

                string[] pathList      = solutionsToImportFolder.Split(new Char [] { '\\' });
                string   DirectoryName = pathList[pathList.Count <string>() - 1];
                LogManager.WriteLog("Importing solutions from " + DirectoryName);

                MSCRMConnection connection = profile.getTargetConneciton();
                _serviceProxy = cm.connect(connection);
                LogManager.WriteLog("Start importing solutions in " + connection.ConnectionName);

                foreach (string solutionArchiveName in solutionsArchivesNames)
                {
                    bool selectedsolutionfound = false;
                    foreach (string solutionname in profile.SelectedSolutionsNames)
                    {
                        if (Path.GetFileName(solutionArchiveName).StartsWith(solutionname))
                        {
                            selectedsolutionfound = true;
                        }
                    }

                    if (!selectedsolutionfound)
                    {
                        continue;
                    }

                    //Import Solution
                    LogManager.WriteLog("Importing solution archive " + Path.GetFileName(solutionArchiveName) + " into " + connection.ConnectionName);

                    byte[] fileBytes = File.ReadAllBytes(solutionArchiveName);

                    Guid ImportJobId = Guid.NewGuid();

                    ImportSolutionRequest impSolReqWithMonitoring = new ImportSolutionRequest()
                    {
                        CustomizationFile = fileBytes,
                        OverwriteUnmanagedCustomizations = profile.OverwriteUnmanagedCustomizations,
                        PublishWorkflows = profile.PublishWorkflows,
                        ImportJobId      = ImportJobId
                    };

                    _serviceProxy.Execute(impSolReqWithMonitoring);


                    Entity ImportJob = _serviceProxy.Retrieve("importjob", impSolReqWithMonitoring.ImportJobId, new ColumnSet(true));
                    //File.WriteAllText(solutionsToImportFolder + "\\importlog_ORIGINAL_" + Path.GetFileNameWithoutExtension(solutionArchiveName) + ".xml", (string)ImportJob["data"]);

                    RetrieveFormattedImportJobResultsRequest importLogRequest = new RetrieveFormattedImportJobResultsRequest()
                    {
                        ImportJobId = ImportJobId
                    };
                    RetrieveFormattedImportJobResultsResponse importLogResponse = (RetrieveFormattedImportJobResultsResponse)_serviceProxy.Execute(importLogRequest);

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

                    string exportedSolutionFileName = solutionsToImportFolder + "\\importlog_" + Path.GetFileNameWithoutExtension(solutionArchiveName) + "_" + timeNow + ".xml";

                    //Fix bad Status and Message export
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.LoadXml((string)ImportJob["data"]);
                    String SolutionImportResult = doc.SelectSingleNode("//solutionManifest/result/@result").Value;

                    File.WriteAllText(exportedSolutionFileName, importLogResponse.FormattedResults);

                    LogManager.WriteLog("Solution " + Path.GetFileName(solutionArchiveName) + " was imported with success in " + connection.ConnectionName);
                }

                //Check if customizations are to be published
                if (profile.PublishAllCustomizationsTarget)
                {
                    LogManager.WriteLog("Publishing all Customizations on " + connection.ConnectionName + " ...");
                    PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
                    _serviceProxy.Execute(publishRequest);
                }

                LogManager.WriteLog("Solutions Import finished for Profile: " + profile.ProfileName);
            }
            else
            {
                LogManager.WriteLog("There are no solutions for import.");
                return;
            }
        }
        /// <summary>
        /// Imports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        private void Import(MSCRMSolutionsTransportProfile profile)
        {
            //Check if there is a solutions to import
            if (Directory.Exists(profile.SolutionExportFolder))
            {
                IOrderedEnumerable<string> subDirectories = Directory.GetDirectories(profile.SolutionExportFolder).OrderByDescending(x => x);
                if (subDirectories.Count<string>() == 0)
                {
                    LogManager.WriteLog("There are no solutions for import.");
                    return;
                }

                //Check which solutions to import: Newest, Oldest, specific exprot date solutions
                string solutionsToImportFolder = "";
                if (profile.SolutionsToImport == "Newest")
                    solutionsToImportFolder = subDirectories.ElementAt(0);
                else if (profile.SolutionsToImport == "Oldest")
                    solutionsToImportFolder = subDirectories.ElementAt(subDirectories.Count<string>() - 1);
                else
                    solutionsToImportFolder = subDirectories.First(s => s.EndsWith(profile.SolutionsToImport));

                if (solutionsToImportFolder == "")
                {
                    LogManager.WriteLog("The specified solutions to import were not found.");
                    return;
                }

                //get all solutions archives
                string[] solutionsArchivesNames = Directory.GetFiles(solutionsToImportFolder, "*.zip");
                if (solutionsArchivesNames.Count<string>() == 0)
                {
                    LogManager.WriteLog("There are no solutions for import.");
                    return;
                }

                string[] pathList = solutionsToImportFolder.Split(new Char [] {'\\'});
                string DirectoryName = pathList[pathList.Count<string>() -1];
                LogManager.WriteLog("Importing solutions from " + DirectoryName);

                MSCRMConnection connection = profile.getTargetConneciton();
                _serviceProxy = cm.connect(connection);
                LogManager.WriteLog("Start importing solutions in " + connection.ConnectionName);

                foreach (string solutionArchiveName in solutionsArchivesNames)
                {
                    bool selectedsolutionfound = false;
                    foreach (string solutionname in profile.SelectedSolutionsNames)
                    {
                        if (Path.GetFileName(solutionArchiveName).StartsWith(solutionname))
                            selectedsolutionfound = true;
                    }

                    if (!selectedsolutionfound)
                        continue;

                    //Import Solution
                    LogManager.WriteLog("Importing solution archive " + Path.GetFileName(solutionArchiveName) + " into " + connection.ConnectionName);

                    byte[] fileBytes = File.ReadAllBytes(solutionArchiveName);

                    Guid ImportJobId = Guid.NewGuid();

                    ImportSolutionRequest impSolReqWithMonitoring = new ImportSolutionRequest()
                    {
                        CustomizationFile = fileBytes,
                        OverwriteUnmanagedCustomizations = profile.OverwriteUnmanagedCustomizations,
                        PublishWorkflows = profile.PublishWorkflows,
                        ImportJobId = ImportJobId
                    };

                    _serviceProxy.Execute(impSolReqWithMonitoring);

                    Entity ImportJob = _serviceProxy.Retrieve("importjob", impSolReqWithMonitoring.ImportJobId, new ColumnSet(true));
                    //File.WriteAllText(solutionsToImportFolder + "\\importlog_ORIGINAL_" + Path.GetFileNameWithoutExtension(solutionArchiveName) + ".xml", (string)ImportJob["data"]);

                    RetrieveFormattedImportJobResultsRequest importLogRequest = new RetrieveFormattedImportJobResultsRequest()
                    {
                        ImportJobId = ImportJobId
                    };
                    RetrieveFormattedImportJobResultsResponse importLogResponse = (RetrieveFormattedImportJobResultsResponse)_serviceProxy.Execute(importLogRequest);

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

                    string exportedSolutionFileName = solutionsToImportFolder + "\\importlog_" + Path.GetFileNameWithoutExtension(solutionArchiveName) + "_" + timeNow + ".xml";

                    //Fix bad Status and Message export
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.LoadXml((string)ImportJob["data"]);
                    String SolutionImportResult = doc.SelectSingleNode("//solutionManifest/result/@result").Value;

                    File.WriteAllText(exportedSolutionFileName, importLogResponse.FormattedResults);

                    LogManager.WriteLog("Solution " + Path.GetFileName(solutionArchiveName) + " was imported with success in " + connection.ConnectionName);
                }

                //Check if customizations are to be published
                if (profile.PublishAllCustomizationsTarget)
                {
                    LogManager.WriteLog("Publishing all Customizations on " + connection.ConnectionName + " ...");
                    PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
                    _serviceProxy.Execute(publishRequest);
                }

                LogManager.WriteLog("Solutions Import finished for Profile: " + profile.ProfileName);
            }
            else
            {
                LogManager.WriteLog("There are no solutions for import.");
                return;
            }
        }