private async Task <string> CreatePluginDescription(Model.ConnectionData connectionData, IOrganizationServiceExtented service, Model.CommonConfiguration commonConfig)
        {
            Model.Backup.PluginDescription description = await GetPluginDescription(service);

            if (connectionData.User != null)
            {
                description.CRMConnectionUserName = connectionData.User.Username;
            }
            else if (CredentialCache.DefaultNetworkCredentials != null)
            {
                description.CRMConnectionUserName =
                    (!string.IsNullOrEmpty(CredentialCache.DefaultNetworkCredentials.Domain) ? CredentialCache.DefaultNetworkCredentials.Domain + "\\" : "")
                    + CredentialCache.DefaultNetworkCredentials.UserName
                ;
            }

            description.Organization          = connectionData.UniqueOrgName;
            description.DiscoveryService      = connectionData.DiscoveryUrl;
            description.OrganizationService   = connectionData.OrganizationUrl;
            description.PublicUrl             = connectionData.PublicUrl;
            description.MachineName           = Environment.MachineName;
            description.ExecuteUserDomainName = Environment.UserDomainName;
            description.ExecuteUserName       = Environment.UserName;

            commonConfig.CheckFolderForExportExists(this._iWriteToOutput);

            string fileName = string.Format("{0}.{1} {2}.xml"
                                            , connectionData.Name
                                            , commonConfig.PluginConfigurationFileName.Trim()
                                            , EntityFileNameFormatter.GetDateString()
                                            );

            string filePath = Path.Combine(commonConfig.FolderForExport, fileName);

            description.Save(filePath);

            this._iWriteToOutput.WriteToOutput(connectionData, "Plugin Description exported to {0}", filePath);
            this._iWriteToOutput.WriteToOutputFilePathUri(connectionData, filePath);

            return(filePath);
        }
        private async Task <Model.Backup.PluginDescription> GetPluginDescription(IOrganizationServiceExtented service)
        {
            var repositoryAssembly = new PluginAssemblyRepository(service);
            var repositoryType     = new PluginTypeRepository(service);

            var repositoryMessage = new SdkMessageRepository(service);
            var repositoryFilter  = new SdkMessageFilterRepository(service);
            var repositorySecure  = new SdkMessageProcessingStepSecureConfigRepository(service);
            var repositoryImage   = new SdkMessageProcessingStepImageRepository(service);

            var repositoryStep = new SdkMessageProcessingStepRepository(service);

            var result = new Model.Backup.PluginDescription();

            result.CreatedOn = DateTime.Now;

            var listAssemblies = await repositoryAssembly.GetAllPluginAssemblisWithStepsAsync();

            var listMessage = await repositoryMessage.GetAllSdkMessageWithStepsAsync();

            var listFilter = await repositoryFilter.GetAllSdkMessageFilterWithStepsAsync();

            var listSecure = await repositorySecure.GetAllSdkMessageProcessingStepSecureConfigAsync();

            foreach (var entAssembly in listAssemblies)
            {
                var assembly = Model.Backup.PluginAssembly.GetObject(entAssembly);

                result.PluginAssemblies.Add(assembly);

                var listTypes = await repositoryType.GetPluginTypesAsync(entAssembly.Id);

                foreach (var entPluginType in listTypes)
                {
                    var pluginType = Model.Backup.PluginType.GetObject(entPluginType);

                    assembly.PluginTypes.Add(pluginType);

                    var listSteps = await repositoryStep.GetPluginStepsByPluginTypeIdAsync(entPluginType.Id);

                    var listStepsToAdd = new List <Model.Backup.PluginStep>();

                    foreach (var entStep in listSteps)
                    {
                        Entities.SdkMessage       entMessage = null;
                        Entities.SdkMessageFilter entFilter  = null;
                        Entities.SdkMessageProcessingStepSecureConfig entSecure = null;

                        var refMessage = entStep.SdkMessageId;
                        if (refMessage != null)
                        {
                            entMessage = listMessage.FirstOrDefault(m => m.SdkMessageId == refMessage.Id);
                        }

                        var refFilter = entStep.SdkMessageFilterId;
                        if (refFilter != null)
                        {
                            entFilter = listFilter.FirstOrDefault(f => f.SdkMessageFilterId == refFilter.Id);
                        }

                        var refSecure = entStep.SdkMessageProcessingStepSecureConfigId;
                        if (refSecure != null)
                        {
                            entSecure = listSecure.FirstOrDefault(s => s.SdkMessageProcessingStepSecureConfigId == refSecure.Id);
                        }

                        var step = Model.Backup.PluginStep.GetObject(entStep, entMessage, entFilter, entSecure);

                        listStepsToAdd.Add(step);

                        var listImages = await repositoryImage.GetStepImagesAsync(entStep.Id);

                        foreach (var entImage in listImages)
                        {
                            var image = Model.Backup.PluginImage.GetObject(entImage);

                            step.PluginImages.Add(image);
                        }
                    }

                    pluginType.PluginSteps.AddRange(
                        listStepsToAdd
                        .OrderBy(step => step.PrimaryEntity)
                        .ThenBy(step => step.SecondaryEntity)
                        .ThenBy(step => step.Message, MessageComparer.Comparer)
                        .ThenBy(step => step.Stage)
                        .ThenBy(step => step.ExecutionOrder)
                        .ThenBy(step => step.Name)
                        .ThenBy(step => step.CreatedOn)
                        );
                }
            }

            return(result);
        }