using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Messages;
/// <summary>
/// 全局选项集
/// </summary>
public class OptionSetMetadataHelper
{
public IOrganizationService service = null;
public Guid optionSetId = Guid.Empty;
public int languageCode = 2052;
/// <summary>
/// 创建全局选项集
/// </summary>
public void Create()
{
#region OptionMetadataCollection
OptionMetadataCollection opCollection = new OptionMetadataCollection();
opCollection.Add(new OptionMetadata(new Label("2000年", languageCode), 2000));
opCollection.Add(new OptionMetadata(new Label("2001年", languageCode), 2001));
opCollection.Add(new OptionMetadata(new Label("2002年", languageCode), 2002));
opCollection.Add(new OptionMetadata(new Label("2003年", languageCode), 2003));
opCollection.Add(new OptionMetadata(new Label("2004年", languageCode), 2004));
opCollection.Add(new OptionMetadata(new Label("2005年", languageCode), 2005));
#endregion
OptionSetMetadata optionSet = new OptionSetMetadata(opCollection);
optionSet.Name = "new_year";
optionSet.Description = new Label("年份", languageCode);
optionSet.DisplayName = new Label("年份", languageCode);
optionSet.IsGlobal = true;
optionSet.OptionSetType = OptionSetType.Picklist;
CreateOptionSetRequest request = new CreateOptionSetRequest();
request.OptionSet = optionSet;
CreateOptionSetResponse response = (CreateOptionSetResponse)service.Execute(request);
optionSetId = response.OptionSetId;
}
/// <summary>
/// 插入新全局选项集选项
/// </summary>
public void InsertOptionValue()
{
InsertOptionValueRequest request = new InsertOptionValueRequest();
request.OptionSetName = "new_year";
request.Label = new Label("2008年", languageCode);
request.Value = 2008;
InsertOptionValueResponse response = (InsertOptionValueResponse)service.Execute(request);
}
/// <summary>
/// 更改选项集中选项的相对顺序
/// </summary>
public void OrderOption()
{
OrderOptionRequest request = new OrderOptionRequest();
request.OptionSetName = "new_year";
request.Values = new int[]{ 2005,2004,2003,2002,2001,2000 };
OrderOptionResponse response = (OrderOptionResponse)service.Execute(request);
}
/// <summary>
/// 检索所有全局选项集
/// </summary>
public void RetrieveAllOptionSets()
{
RetrieveAllOptionSetsRequest request = new RetrieveAllOptionSetsRequest();
request.RetrieveAsIfPublished = true;
RetrieveAllOptionSetsResponse response = (RetrieveAllOptionSetsResponse)service.Execute(request);
OptionSetMetadataBase[] optionSetMetadata = response.OptionSetMetadata;
}
/// <summary>
/// 检索全局选项集
/// </summary>
/// <param name="name">选项集的名称</param>
public void RetrieveOptionSet(string name)
{
RetrieveOptionSetRequest request = new RetrieveOptionSetRequest();
request.Name = name;
request.RetrieveAsIfPublished = true;
RetrieveOptionSetResponse response = (RetrieveOptionSetResponse)service.Execute(request);
OptionSetMetadataBase optionSetMetadata = response.OptionSetMetadata;
}
/// <summary>
/// 删除全局选项集中的项
/// </summary>
/// <param name="v">值</param>
public void DeleteItem(int v)
{
DeleteOptionValueRequest request = new DeleteOptionValueRequest();
request.OptionSetName = "new_year";
request.Value = v;
DeleteOptionValueResponse response = (DeleteOptionValueResponse)service.Execute(request);
}
/// <summary>
/// 删除全局选项集
/// </summary>
public void Delete()
{
DeleteOptionSetRequest request = new DeleteOptionSetRequest();
request.Name = "new_year";
DeleteOptionSetResponse response = (DeleteOptionSetResponse)service.Execute(request);
}
}