using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
/// <summary>
/// 创建Picklist类型的字段
/// </summary>
public class CreatePicklistAttributeHelper
{
private string entityName = "new_class";
public void Create(IOrganizationService service)
{
CreateAttributeRequest request = new CreateAttributeRequest();
//关联的实体名称
request.EntityName = entityName;
PicklistAttributeMetadata pickAttr = new PicklistAttributeMetadata();
//字段名称
pickAttr.LogicalName = "new_picklistvalue";
//架构名称
pickAttr.SchemaName = "new_picklistvalue";
//显示中文名称
pickAttr.DisplayName = new Label("动作", 2052);
//描述
pickAttr.Description = new Label("下拉框", 2052);
//需求级别
pickAttr.RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
//字段安全性
pickAttr.IsSecured = false;
//审核
pickAttr.IsAuditEnabled = new BooleanManagedProperty(false);
//选项
OptionSetMetadata optionSet = new OptionSetMetadata();
optionSet.IsGlobal = false;
optionSet.OptionSetType = OptionSetType.Picklist;
optionSet.Options.Add(new OptionMetadata(new Label("Create", 2052), 1));
optionSet.Options.Add(new OptionMetadata(new Label("Update", 2052), 2));
optionSet.Options.Add(new OptionMetadata(new Label("Delete", 2052), 3));
pickAttr.OptionSet = optionSet;
//设置默认值
pickAttr.DefaultFormValue = 1;
request.Attribute = pickAttr;
service.Execute(request);
}
}