コード例 #1
0
        public static TResponse Execute <TRequest, TResponse>(TRequest request, CrmConnection connection = null)
            where TRequest : OrganizationRequest
            where TResponse : OrganizationResponse
        {
            OrganizationService srv = new OrganizationService(connection ?? XrmConnection.Connection);

            using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(srv))
            {
                return((TResponse)service.Execute(request));
            }
        }
コード例 #2
0
        private void ImportSolution(CrmConnection connection)
        {
            if (string.IsNullOrWhiteSpace(this.Extension))
            {
                Log.LogError("Required parameter missing: Extension");
                return;
            }

            string directoryPath = string.IsNullOrEmpty(this.Path)
                ? System.IO.Path.GetDirectoryName(this.BuildEngine.ProjectFileOfTaskNode)
                : this.Path;

            // ReSharper disable once AssignNullToNotNullAttribute
            string solutioneFile = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", System.IO.Path.Combine(directoryPath, this.Name), this.Extension);

            if (!File.Exists(solutioneFile))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The given Solution file for import does not exist. {0}", solutioneFile));
                return;
            }

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled;
                    });

                    byte[] customizationFile = File.ReadAllBytes(solutioneFile);
                    var    request           = new ImportSolutionRequest
                    {
                        CustomizationFile = customizationFile,
                        OverwriteUnmanagedCustomizations = this.overwriteCustomizations,
                        PublishWorkflows = this.EnableSdkProcessingSteps
                    };

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing Solution {0}. Please wait...", this.Name));
                    serviceContext.Execute(request);
                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported Solution {0} to organization with Url {1}.", this.Name, this.OrganizationUrl));
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "An error occurred while importing Solution {0} to Organization with Url {1}. [{2}]",
                                     this.Name,
                                     this.OrganizationUrl,
                                     exception.Message));
                }
            }
        }
コード例 #3
0
        private void ExportSolution(CrmConnection connection)
        {
            if (string.IsNullOrWhiteSpace(this.Extension))
            {
                Log.LogError("Required parameter missing: Extension");
                return;
            }

            string directoryPath = string.IsNullOrEmpty(this.Path)
                ? System.IO.Path.GetDirectoryName(this.BuildEngine.ProjectFileOfTaskNode)
                : this.Path;

            // ReSharper disable once AssignNullToNotNullAttribute
            string solutioneFile = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", System.IO.Path.Combine(directoryPath, this.Name), this.Extension);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var exportSolutionRequest = new ExportSolutionRequest
                    {
                        SolutionName = this.Name,
                        Managed      = this.ExportAsManagedSolution
                    };

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Exporting Solution {0}. Please wait...", this.Name));
                    var response = serviceContext.Execute(exportSolutionRequest) as ExportSolutionResponse;
                    if (response == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "An error occurred in in exporting Solution {0} from organization with Url {1}.", this.Name, this.OrganizationUrl));
                    }
                    else
                    {
                        byte[] exportSolutionFileContentsBytes = response.ExportSolutionFile;
                        File.WriteAllBytes(solutioneFile, exportSolutionFileContentsBytes);
                        Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully exported Solution {0} from organization with Url {1}.", this.Name, this.OrganizationUrl));
                    }
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "An error occurred while exporting Solution {0} from Organization with Url {1}. [{2}]",
                                     this.Name,
                                     this.OrganizationUrl,
                                     exception.Message));
                }
            }
        }
コード例 #4
0
        private void ImportDataMap()
        {
            if (string.IsNullOrWhiteSpace(this.FilePath))
            {
                this.Log.LogError("Required parameter missing: FilePath");
                return;
            }

            if (!File.Exists(this.FilePath))
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find Data Map file {0}", this.FilePath));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var    connection       = CrmConnection.Parse(connectionString);
            var    mapText          = File.ReadAllText(this.FilePath);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var request = new ImportMappingsImportMapRequest
                    {
                        MappingsXml = mapText,
                        ReplaceIds  = true
                    };

                    if (this.overwrite)
                    {
                        DeleteDataMap(serviceContext, this.Name);
                    }

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing Data Map {0}", this.Name));
                    serviceContext.Execute(request);
                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported Data Map {0} to organization with Url {1}.", this.Name, this.OrganizationUrl));
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "An error occurred while importing Data Map {0} to Organization with Url {1}. [{2}]",
                                     this.Name,
                                     this.OrganizationUrl,
                                     exception.Message));
                }
            }
        }
コード例 #5
0
 public static void AddRelated(Entity PrimaryEntity, EntityCollection RelatedEntities, string RelationshipName, CrmConnection connection = null)
 {
     using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(connection ?? XrmConnection.Connection))
     {
         EntityReferenceCollection col = new EntityReferenceCollection();
         foreach (Entity ent in RelatedEntities.Entities)
         {
             col.Add(ent.ToEntityReference());
         }
         AssociateRequest request = new AssociateRequest()
         {
             Target          = PrimaryEntity.ToEntityReference(),
             RelatedEntities = col,
             Relationship    = new Relationship(RelationshipName)
         };
         AssociateResponse response = (AssociateResponse)service.Execute(request);
     }
 }
コード例 #6
0
ファイル: Data.cs プロジェクト: zxw-ing/MSBuildExtensionPack
        private void ImportData()
        {
            if (!File.Exists(this.FilePath))
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find import data file {0}", this.FilePath));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var    connection       = CrmConnection.Parse(connectionString);

            string content = File.ReadAllText(this.FilePath);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var dataMap = serviceContext.CreateQuery("importmap").FirstOrDefault(s => s.GetAttributeValue <string>("name") == this.DataMapName);
                    if (dataMap == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find data map {0} in the organization with Url {1}", this.DataMapName, this.OrganizationUrl));
                        return;
                    }

                    var importId = CreateImportEntity(serviceContext, this.SourceEntityName);
                    this.CreateImportFileEntity(serviceContext, content, importId, dataMap.Id);

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing data from File {0} to entity {1}", this.FilePath, this.TargetEntityName));
                    serviceContext.Execute(new ParseImportRequest {
                        ImportId = importId
                    });
                    serviceContext.Execute(new TransformImportRequest {
                        ImportId = importId
                    });
                    serviceContext.Execute(new ImportRecordsImportRequest {
                        ImportId = importId
                    });
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled;
                    });

                    int  waitCount       = 0;
                    bool importCompleted = false;
                    do
                    {
                        int statusCode = GetImportStatus(serviceContext, importId);
                        switch (statusCode)
                        {
                        case DataImportStatusSuccess:
                            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported data file {0} to entity {1}.", this.FilePath, this.TargetEntityName));
                            importCompleted = true;
                            break;

                        case DataImportStatusFailed:
                            Log.LogError(string.Format(CultureInfo.CurrentCulture, "Import of data file {0} to entity {1} failed.", this.FilePath, this.TargetEntityName));
                            importCompleted = true;
                            break;
                        }

                        if (!importCompleted)
                        {
                            Log.LogMessage("Importing...");

                            Thread.Sleep(WaitIntervalInMilliseconds);
                            if (++waitCount > this.timeoutInMinutes)
                            {
                                Log.LogError("Import failed to complete during the maximum allocated time");
                                break;
                            }
                        }
                    }while (!importCompleted);
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "An error occurred while importing Data file {0} to Entity {1} for Organization with Url {2}. [{3}]",
                                     this.FilePath,
                                     this.TargetEntityName,
                                     this.OrganizationUrl,
                                     exception.Message));
                }
            }
        }
コード例 #7
0
        private void ExportSolution(CrmConnection connection)
        {
            if (string.IsNullOrWhiteSpace(this.Extension))
            {
                Log.LogError("Required parameter missing: Extension");
                return;
            }

            string directoryPath = string.IsNullOrEmpty(this.Path)
                ? System.IO.Path.GetDirectoryName(this.BuildEngine.ProjectFileOfTaskNode)
                : this.Path;

            // ReSharper disable once AssignNullToNotNullAttribute
            string solutioneFile = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", System.IO.Path.Combine(directoryPath, this.Name), this.Extension);
            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var exportSolutionRequest = new ExportSolutionRequest
                                                {
                                                    SolutionName = this.Name,
                                                    Managed = this.ExportAsManagedSolution
                                                };

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Exporting Solution {0}. Please wait...", this.Name));
                    var response = serviceContext.Execute(exportSolutionRequest) as ExportSolutionResponse;
                    if (response == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "An error occurred in in exporting Solution {0} from organization with Url {1}.", this.Name, this.OrganizationUrl));    
                    }
                    else
                    {
                        byte[] exportSolutionFileContentsBytes = response.ExportSolutionFile;
                        File.WriteAllBytes(solutioneFile, exportSolutionFileContentsBytes);
                        Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully exported Solution {0} from organization with Url {1}.", this.Name, this.OrganizationUrl));    
                    }
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while exporting Solution {0} from Organization with Url {1}. [{2}]",
                                    this.Name,
                                    this.OrganizationUrl,
                                    exception.Message));
                }
            }
        }
コード例 #8
0
        private void ImportSolution(CrmConnection connection)
        {
            if (string.IsNullOrWhiteSpace(this.Extension))
            {
                Log.LogError("Required parameter missing: Extension");
                return;
            }

            string directoryPath = string.IsNullOrEmpty(this.Path)
                ? System.IO.Path.GetDirectoryName(this.BuildEngine.ProjectFileOfTaskNode)
                : this.Path;

            // ReSharper disable once AssignNullToNotNullAttribute
            string solutioneFile = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", System.IO.Path.Combine(directoryPath, this.Name), this.Extension);
            if (!File.Exists(solutioneFile))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The given Solution file for import does not exist. {0}", solutioneFile));
                return;
            }

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled;
                    });

                    byte[] customizationFile = File.ReadAllBytes(solutioneFile);
                    var request = new ImportSolutionRequest
                    {
                        CustomizationFile = customizationFile,
                        OverwriteUnmanagedCustomizations = this.overwriteCustomizations,
                        PublishWorkflows = this.EnableSdkProcessingSteps
                    };

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing Solution {0}. Please wait...", this.Name));
                    serviceContext.Execute(request);
                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported Solution {0} to organization with Url {1}.", this.Name, this.OrganizationUrl));
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while importing Solution {0} to Organization with Url {1}. [{2}]",
                                    this.Name, 
                                    this.OrganizationUrl,
                                    exception.Message));
                }
            }
        }
コード例 #9
0
        private void ImportData()
        {
            if (!File.Exists(this.FilePath))
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find import data file {0}", this.FilePath));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var connection = CrmConnection.Parse(connectionString);

            string content = File.ReadAllText(this.FilePath);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var dataMap = serviceContext.CreateQuery("importmap").FirstOrDefault(s => s.GetAttributeValue<string>("name") == this.DataMapName);
                    if (dataMap == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find data map {0} in the organization with Url {1}", this.DataMapName, this.OrganizationUrl));
                        return;
                    }

                    var importId = CreateImportEntity(serviceContext, this.SourceEntityName);
                    this.CreateImportFileEntity(serviceContext, content, importId, dataMap.Id);

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing data from File {0} to entity {1}", this.FilePath, this.TargetEntityName));
                    serviceContext.Execute(new ParseImportRequest { ImportId = importId });
                    serviceContext.Execute(new TransformImportRequest { ImportId = importId });
                    serviceContext.Execute(new ImportRecordsImportRequest { ImportId = importId });
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled; 
                    });

                    int waitCount = 0;
                    bool importCompleted = false;
                    do
                    {
                        int statusCode = GetImportStatus(serviceContext, importId);
                        switch (statusCode)
                        {
                            case DataImportStatusSuccess:
                                Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported data file {0} to entity {1}.", this.FilePath, this.TargetEntityName));
                                importCompleted = true;
                                break;

                            case DataImportStatusFailed:
                                Log.LogError(string.Format(CultureInfo.CurrentCulture, "Import of data file {0} to entity {1} failed.", this.FilePath, this.TargetEntityName));
                                importCompleted = true;
                                break;
                        }

                        if (!importCompleted)
                        {
                            Log.LogMessage("Importing...");

                            Thread.Sleep(WaitIntervalInMilliseconds);
                            if (++waitCount > this.timeoutInMinutes)
                            {
                                Log.LogError("Import failed to complete during the maximum allocated time");
                                break;
                            }                            
                        }
                    } 
                    while (!importCompleted);
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while importing Data file {0} to Entity {1} for Organization with Url {2}. [{3}]",
                                    this.FilePath,
                                    this.TargetEntityName,
                                    this.OrganizationUrl,
                                    exception.Message));
                }
            }
        }
コード例 #10
0
        private void UpdateSettings()
        {
            if (string.IsNullOrWhiteSpace(this.OrganizationUrl) || !Uri.IsWellFormedUriString(this.OrganizationUrl, UriKind.Absolute))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The Organization Url is not valid. {0}", this.OrganizationUrl));
                return;
            }

            if (this.Settings == null)
            {
                Log.LogError("Required parameter missing: Settings");
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var connection = CrmConnection.Parse(connectionString);
            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var request = new RetrieveEntityRequest
                    {
                        EntityFilters = EntityFilters.Attributes,
                        LogicalName = "organization"
                    };

                    var response = serviceContext.Execute(request) as RetrieveEntityResponse;
                    if (response == null)
                    {
                        Log.LogError(string.Format(
                                        CultureInfo.CurrentCulture,
                                        "No response was received while retrieving settings for Organization with Url {0}",
                                        this.OrganizationUrl));
                        return;
                    }

                    var columnSet = new ColumnSet();
                    foreach (var settingItem in this.Settings)
                    {
                        string settingName = settingItem.ItemSpec;
                        columnSet.AddColumn(settingName);
                        var setting = response.EntityMetadata.Attributes.First(e => e.LogicalName == settingName);
                        if (setting == null || setting.AttributeType == null)
                        {
                            Log.LogError(string.Format(
                                            CultureInfo.CurrentCulture,
                                            "No meta data for setting {0} was found.",
                                            settingName));
                            return;
                        }
                    }

                    var entityCollection = serviceContext.RetrieveMultiple(
                        new QueryExpression("organization")
                        {
                            ColumnSet = columnSet
                        });

                    if (entityCollection == null || entityCollection.Entities.Count == 0)
                    {
                        Log.LogError(string.Format(
                                        CultureInfo.CurrentCulture,
                                        "No setting was found for one of the settings"));
                        return;
                    }

                    var entity = entityCollection.Entities.First();
                    foreach (var settingItem in this.Settings)
                    {
                        string settingName = settingItem.ItemSpec;
                        string settingValue = settingItem.GetMetadata("value");
                        var setting = response.EntityMetadata.Attributes.First(e => e.LogicalName == settingName);
                        if (setting == null || setting.AttributeType == null)
                        {
                            Log.LogError(string.Format(
                                            CultureInfo.CurrentCulture,
                                            "No meta data was found for setting with Name {0} was found.", 
                                            settingName));
                            return;
                        }

                        entity.Attributes[settingName] = ConvertCrmTypeToDotNetType(setting.AttributeType.Value, settingValue);
                    }

                    serviceContext.Update(entity);
                    Log.LogMessage(MessageImportance.High, "The organization settings were updated successfully.");   
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(CultureInfo.CurrentCulture, "An error occurred while update settings for Organization with Url {0}. [{1}]", this.OrganizationUrl, exception.Message));   
                }
            }
        }
コード例 #11
0
        private void ImportDataMap()
        {
            if (string.IsNullOrWhiteSpace(this.FilePath))
            {
                this.Log.LogError("Required parameter missing: FilePath");
                return;
            }

            if (!File.Exists(this.FilePath))
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find Data Map file {0}", this.FilePath));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var connection = CrmConnection.Parse(connectionString);
            var mapText = File.ReadAllText(this.FilePath);
            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var request = new ImportMappingsImportMapRequest
                    {
                        MappingsXml  = mapText,
                        ReplaceIds = true
                    };

                    if (this.overwrite)
                    {
                        DeleteDataMap(serviceContext, this.Name);
                    }

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing Data Map {0}", this.Name));
                    serviceContext.Execute(request);
                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported Data Map {0} to organization with Url {1}.", this.Name, this.OrganizationUrl));
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while importing Data Map {0} to Organization with Url {1}. [{2}]",
                                    this.Name,
                                    this.OrganizationUrl,
                                    exception.Message));
                }
            }
        }
コード例 #12
0
        private void UpdateSettings()
        {
            if (string.IsNullOrWhiteSpace(this.OrganizationUrl) || !Uri.IsWellFormedUriString(this.OrganizationUrl, UriKind.Absolute))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The Organization Url is not valid. {0}", this.OrganizationUrl));
                return;
            }

            if (this.Settings == null)
            {
                Log.LogError("Required parameter missing: Settings");
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var    connection       = CrmConnection.Parse(connectionString);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var request = new RetrieveEntityRequest
                    {
                        EntityFilters = EntityFilters.Attributes,
                        LogicalName   = "organization"
                    };

                    var response = serviceContext.Execute(request) as RetrieveEntityResponse;
                    if (response == null)
                    {
                        Log.LogError(string.Format(
                                         CultureInfo.CurrentCulture,
                                         "No response was received while retrieving settings for Organization with Url {0}",
                                         this.OrganizationUrl));
                        return;
                    }

                    var columnSet = new ColumnSet();
                    foreach (var settingItem in this.Settings)
                    {
                        string settingName = settingItem.ItemSpec;
                        columnSet.AddColumn(settingName);
                        var setting = response.EntityMetadata.Attributes.First(e => e.LogicalName == settingName);
                        if (setting == null || setting.AttributeType == null)
                        {
                            Log.LogError(string.Format(
                                             CultureInfo.CurrentCulture,
                                             "No meta data for setting {0} was found.",
                                             settingName));
                            return;
                        }
                    }

                    var entityCollection = serviceContext.RetrieveMultiple(
                        new QueryExpression("organization")
                    {
                        ColumnSet = columnSet
                    });

                    if (entityCollection == null || entityCollection.Entities.Count == 0)
                    {
                        Log.LogError(string.Format(
                                         CultureInfo.CurrentCulture,
                                         "No setting was found for one of the settings"));
                        return;
                    }

                    var entity = entityCollection.Entities.First();
                    foreach (var settingItem in this.Settings)
                    {
                        string settingName  = settingItem.ItemSpec;
                        string settingValue = settingItem.GetMetadata("value");
                        var    setting      = response.EntityMetadata.Attributes.First(e => e.LogicalName == settingName);
                        if (setting == null || setting.AttributeType == null)
                        {
                            Log.LogError(string.Format(
                                             CultureInfo.CurrentCulture,
                                             "No meta data was found for setting with Name {0} was found.",
                                             settingName));
                            return;
                        }

                        entity.Attributes[settingName] = ConvertCrmTypeToDotNetType(setting.AttributeType.Value, settingValue);
                    }

                    serviceContext.Update(entity);
                    Log.LogMessage(MessageImportance.High, "The organization settings were updated successfully.");
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(CultureInfo.CurrentCulture, "An error occurred while update settings for Organization with Url {0}. [{1}]", this.OrganizationUrl, exception.Message));
                }
            }
        }