Exemplo n.º 1
0
        public CRMEntityMetadata CRMGetEntityMetadata(CRMEntityMetadata entitytype)
        {
            OrganizationServiceProxy _serviceProxy;

            Microsoft.Xrm.Sdk.Metadata.EntityFilters filter = entitytype.IncludeAttributes ? Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes : Microsoft.Xrm.Sdk.Metadata.EntityFilters.Entity;

            using (_serviceProxy = GetCRMConnection())
            {
                try
                {
                    RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
                    {
                        EntityFilters = filter,
                        LogicalName   = entitytype.LogicalName.ToLower()
                    };

                    RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(retrieveEntityRequest);
                    EntityMetadata         entityMetadata         = retrieveEntityResponse.EntityMetadata;

                    entitytype.DisplayName = entityMetadata.DisplayName.UserLocalizedLabel != null?entityMetadata.DisplayName.UserLocalizedLabel.Label.ToString() : entityMetadata.LogicalName;

                    entitytype.ObjectTypeCode       = entityMetadata.ObjectTypeCode.Value;
                    entitytype.LogicalName          = entityMetadata.LogicalName;
                    entitytype.PrimaryIdAttribute   = entityMetadata.PrimaryIdAttribute;
                    entitytype.PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute;
                    entitytype.IsCustomEntity       = entityMetadata.IsCustomEntity.Value;
                    entitytype.Attributes           = new List <CRMAttribute>();

                    List <CRMAttribute> Attributes = new List <CRMAttribute>();
                    if (entitytype.IncludeAttributes)
                    {
                        foreach (AttributeMetadata metadata in entityMetadata.Attributes)
                        {
                            CRMAttribute attrib = new CRMAttribute();
                            attrib.AttributeType    = metadata.AttributeType.HasValue ? metadata.AttributeType.Value.ToString() : "";
                            attrib.Description      = metadata.Description.UserLocalizedLabel != null ? metadata.Description.UserLocalizedLabel.Label : "";
                            attrib.DisplayName      = metadata.DisplayName.UserLocalizedLabel != null ? metadata.DisplayName.UserLocalizedLabel.Label : metadata.LogicalName;
                            attrib.IsPrimaryId      = metadata.IsPrimaryId.HasValue ? metadata.IsPrimaryId.Value : false;
                            attrib.IsPrimaryName    = metadata.IsPrimaryName.HasValue ? metadata.IsPrimaryName.Value : false;
                            attrib.IsValidForCreate = metadata.IsValidForCreate.HasValue ? metadata.IsValidForCreate.Value : false;
                            attrib.IsValidForRead   = metadata.IsValidForRead.HasValue ? metadata.IsValidForRead.Value : false;
                            attrib.IsValidForUpdate = metadata.IsValidForUpdate.HasValue ? metadata.IsValidForUpdate.Value : false;
                            attrib.LogicalName      = metadata.LogicalName;
                            attrib.RequiredLevel    = metadata.RequiredLevel.Value.ToString();
                            attrib.SchemaName       = metadata.SchemaName;
                            Attributes.Add(attrib);
                        }
                        entitytype.Attributes = Attributes;
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return(entitytype);
        }
        public CRMEntityMetadata CRMGetEntityMetadata(CRMEntityMetadata entitytype)
        {
            OrganizationServiceProxy _serviceProxy;

            Microsoft.Xrm.Sdk.Metadata.EntityFilters filter = entitytype.IncludeAttributes ? Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes : Microsoft.Xrm.Sdk.Metadata.EntityFilters.Entity;

            using (_serviceProxy = GetCRMConnection())
            {
                try
                {
                    RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
                        {
                            EntityFilters = filter,
                            LogicalName = entitytype.LogicalName.ToLower()
                        };

                    RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(retrieveEntityRequest);
                    EntityMetadata entityMetadata = retrieveEntityResponse.EntityMetadata;

                    entitytype.DisplayName = entityMetadata.DisplayName.UserLocalizedLabel != null ? entityMetadata.DisplayName.UserLocalizedLabel.Label.ToString() : entityMetadata.LogicalName;
                    entitytype.ObjectTypeCode = entityMetadata.ObjectTypeCode.Value;
                    entitytype.LogicalName = entityMetadata.LogicalName;
                    entitytype.PrimaryIdAttribute = entityMetadata.PrimaryIdAttribute;
                    entitytype.PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute;
                    entitytype.IsCustomEntity = entityMetadata.IsCustomEntity.Value;
                    entitytype.Attributes = new List<CRMAttribute>();

                    List<CRMAttribute> Attributes = new List<CRMAttribute>();
                    if (entitytype.IncludeAttributes)
                    {
                        foreach (AttributeMetadata metadata in entityMetadata.Attributes)
                        {
                            CRMAttribute attrib = new CRMAttribute();
                            attrib.AttributeType = metadata.AttributeType.HasValue ? metadata.AttributeType.Value.ToString() : "";
                            attrib.Description = metadata.Description.UserLocalizedLabel != null ? metadata.Description.UserLocalizedLabel.Label : "";
                            attrib.DisplayName = metadata.DisplayName.UserLocalizedLabel != null ? metadata.DisplayName.UserLocalizedLabel.Label : metadata.LogicalName;
                            attrib.IsPrimaryId = metadata.IsPrimaryId.HasValue ? metadata.IsPrimaryId.Value : false;
                            attrib.IsPrimaryName = metadata.IsPrimaryName.HasValue ? metadata.IsPrimaryName.Value : false;
                            attrib.IsValidForCreate = metadata.IsValidForCreate.HasValue ? metadata.IsValidForCreate.Value : false;
                            attrib.IsValidForRead = metadata.IsValidForRead.HasValue ? metadata.IsValidForRead.Value : false;
                            attrib.IsValidForUpdate = metadata.IsValidForUpdate.HasValue ? metadata.IsValidForUpdate.Value : false;
                            attrib.LogicalName = metadata.LogicalName;
                            attrib.RequiredLevel = metadata.RequiredLevel.Value.ToString();
                            attrib.SchemaName = metadata.SchemaName;
                            Attributes.Add(attrib);
                        }
                        entitytype.Attributes = Attributes;
                    }

                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return entitytype;
        }
        private Property SetGetEntityAttributeProperties(Property prop, CRMAttribute task)
        {
            switch (prop.Name.ToLower())
            {
                case "attributelogicalname":
                    prop.Value = task.LogicalName;
                    break;
                case "attributeschemaname":
                    prop.Value = task.SchemaName;
                    break;
                case "attributedisplayname":
                    prop.Value = task.DisplayName;
                    break;
                case "attributetype":
                    prop.Value = task.AttributeType;
                    break;
                case "attributedescription":
                    prop.Value = task.Description;
                    break;
                case "attributerequiredlevel":
                    prop.Value = task.RequiredLevel;
                    break;
                case "attributeisvalidforcreate":
                    prop.Value = task.IsValidForCreate;
                    break;
                case "attributeisvalidforread":
                    prop.Value = task.IsValidForRead;
                    break;
                case "attributeisvalidforupdate":
                    prop.Value = task.IsValidForUpdate;
                    break;
            }

            return prop;
        }