首页 > 代码库 > crm2011创建Boolean类型字段

crm2011创建Boolean类型字段

     using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;

    /// <summary>
    /// Boolean
    /// </summary>
    public class CreateBooleanAttributeHelper
    {
        private string entityName = "new_class";

        public void Create(IOrganizationService service)
        {
            CreateAttributeRequest request = new CreateAttributeRequest();
            //关联的实体名称
            request.EntityName = entityName;
            BooleanAttributeMetadata boolAttribute = new BooleanAttributeMetadata();
            //字段名称
            boolAttribute.LogicalName = "new_boolvalue";
            //架构名称
            boolAttribute.SchemaName = "new_boolvalue";
            //显示中文名称
            boolAttribute.DisplayName = new Label("Boolean字段", 2052);
            //描述
            boolAttribute.Description = new Label("Boolean字段", 2052);
            //需求级别
            boolAttribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
            //字段安全性
            boolAttribute.IsSecured = false;
            //审核
            boolAttribute.IsAuditEnabled = new BooleanManagedProperty(false);
            //选项
            BooleanOptionSetMetadata optionSet = new BooleanOptionSetMetadata();
            optionSet.FalseOption = new OptionMetadata(new Label("False", 2052), 0);
            optionSet.TrueOption = new OptionMetadata(new Label("True", 2052), 1);
            boolAttribute.OptionSet = optionSet;
            //默认值
            boolAttribute.DefaultValue = http://www.mamicode.com/false;

            request.Attribute = boolAttribute;

            service.Execute(request);
        }
    }

 

结果: