Beispiel #1
0
        private Entity GetVersionEntity(CrmOrganizationServiceContext serviceContext)
        {
            Entity returnEntity    = null;
            var    queryExpression = new QueryExpression("solution")
            {
                ColumnSet = new ColumnSet(new[] { "version" })
            };

            queryExpression.Criteria.AddCondition("uniquename", ConditionOperator.Equal, new object[] { this.Name });
            try
            {
                var entityCollection = serviceContext.RetrieveMultiple(queryExpression);
                if (entityCollection == null || entityCollection.Entities.Count == 0)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "Unable to retrieve details of Solution {0} from Organization with Url {1}.",
                                     this.Name,
                                     this.OrganizationUrl));
                }
                else
                {
                    returnEntity = entityCollection.Entities[0];
                }
            }
            catch (Exception exception)
            {
                Log.LogError(string.Format(
                                 CultureInfo.CurrentCulture,
                                 "An error occurred while retrieving version details of Solution {0} from Organization with Url {1}. [{2}]",
                                 this.Name,
                                 this.OrganizationUrl,
                                 exception.Message));
            }

            return(returnEntity);
        }
        private Entity GetVersionEntity(CrmOrganizationServiceContext serviceContext)
        {
            Entity returnEntity = null;
            var queryExpression = new QueryExpression("solution")
                                    {
                                        ColumnSet = new ColumnSet(new[] { "version" })
                                    };
            
            queryExpression.Criteria.AddCondition("uniquename", ConditionOperator.Equal, new object[] { this.Name });
            try
            {
                var entityCollection = serviceContext.RetrieveMultiple(queryExpression);
                if (entityCollection == null || entityCollection.Entities.Count == 0)
                {
                    Log.LogError(string.Format(
                        CultureInfo.CurrentCulture,
                        "Unable to retrieve details of Solution {0} from Organization with Url {1}.",
                        this.Name,
                        this.OrganizationUrl));
                }
                else
                {
                    returnEntity = entityCollection.Entities[0];
                }
            }
            catch (Exception exception)
            {
                Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while retrieving version details of Solution {0} from Organization with Url {1}. [{2}]",
                                    this.Name,
                                    this.OrganizationUrl,
                                    exception.Message));
            }

            return returnEntity;
        }
Beispiel #3
0
        public static EntityCollection RetrieveByQuery(QueryExpression query, CrmConnection connection = null, bool CacheResults = true)
        {
            if (CacheResults)
            {
                using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(connection ?? XrmConnection.Connection))
                {
                    query.PageInfo = new PagingInfo()
                    {
                        PageNumber = 1, PagingCookie = null, Count = 5000
                    };
                    EntityCollection ResultCollection = new EntityCollection();
                    EntityCollection col = null;
                    while (true)
                    {
                        try
                        {
                            col = service.RetrieveMultiple(query);
                        }
                        catch (System.ServiceModel.Security.MessageSecurityException ex1)
                        {
                            throw ex1;
                        }
                        catch (FaultException ex)
                        {
                            throw ex;
                        }

                        ResultCollection.EntityName = col.EntityName;
                        ResultCollection.Entities.AddRange(col.Entities);
                        if (col.MoreRecords)
                        {
                            query.PageInfo.PageNumber++;
                            query.PageInfo.PagingCookie = col.PagingCookie;
                        }
                        else
                        {
                            break;
                        }
                    }
                    return(ResultCollection);
                }
            }
            else
            {
                OrganizationService srv = new OrganizationService(connection ?? XrmConnection.Connection);
                using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(srv))
                {
                    query.PageInfo = new PagingInfo()
                    {
                        PageNumber = 1, PagingCookie = null, Count = 5000
                    };
                    EntityCollection ResultCollection = new EntityCollection();
                    while (true)
                    {
                        EntityCollection col = service.RetrieveMultiple(query);
                        ResultCollection.EntityName = col.EntityName;
                        ResultCollection.Entities.AddRange(col.Entities);
                        if (col.MoreRecords)
                        {
                            query.PageInfo.PageNumber++;
                            query.PageInfo.PagingCookie = col.PagingCookie;
                        }
                        else
                        {
                            break;
                        }
                    }
                    return(ResultCollection);
                }
            }
        }
        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));   
                }
            }
        }
        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));
                }
            }
        }