如何在CRM 2011中使用CRM sdk和C#从实体中的字段中获取选项集
本文关键字:CRM 实体 字段 获取 选项 sdk 2011 | 更新日期: 2023-09-27 18:00:19
如何使用CRM sdk和C#从CRM 2011中的实体中的字段中获取选项集?我只想和你们分享一个直接的方法,在一个实体中获得一个字段的选项集。
在Dynamics CRM中检索元数据信息的正确方法是只检索所需的信息。我们应该只检索基于原始问题的选项集值。当所有需求指定的都是选项集的值时,检索实体的所有元数据是不必要的,并且会产生不必要的开销。
以下是获取选项集的选项列表的正确方法。
public static void GetOptionSet(string entityName, string fieldName, IOrganizationService service)
{
var attReq = new RetrieveAttributeRequest();
attReq.EntityLogicalName = entityName;
attReq.LogicalName = fieldName;
attReq.RetrieveAsIfPublished = true;
var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;
var optionList = (from o in attMetadata.OptionSet.Options
select new {Value = o.Value, Text = o.Label.UserLocalizedLabel.Label}).ToList();
}
此方法需要实体名称、包含选项集的字段名称和实例化的IOOrganizationService。
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
public void GetOptionSet(string entityName, string fieldName, IOrganizationService service)
{
RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest();
retrieveDetails.EntityFilters = EntityFilters.All;
retrieveDetails.LogicalName = entityName;
RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, fieldName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
OptionSetMetadata options = picklistMetadata.OptionSet;
var optionlist = (from o in options.Options
select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();
//from here you can do anything you want now with the optionlist
}
参考:
http://guruprasadcrm.blogspot.ae/2011/12/retrieve-optionset-text-in-crm-2011.html
我希望这能对你们中的一些人的项目有所帮助。
嘿,为什么不使用这个??
OptionSetValue CountryOptionSet = Contact.Attributes.Contains("gr_address2_country") ? Contact["gr_address2_country"] as OptionSetValue : null;
if (CountryOptionSet != null)
string Country = CountryOptionSet.Value.ToString();
取决于它是本地的还是全局的。如果是本地的,则:
string optionsetText = entity.FormattedValues["new_optionset"];
如果它是全球性的,那么你需要更多的代码:
public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
{
string AttributeName = attributeName;
string EntityLogicalName = entityName;
RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.All,
LogicalName = EntityLogicalName
};
RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
IList<OptionMetadata> OptionsList = (from o in options.Options
where o.Value.Value == optionSetValue
select o).ToList();
string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
return optionsetLabel;
我从Guido那里得到了另一个类似的问题,但rcados缺少第三个参数。要获取文本,只需设置
var foo = GetoptionsetText("lead", "picklist", 1234 , service)
假设您已经声明IoorganizationService
有两种方法可以获得OptionSet的文本:
第一:
OptionSetValue opProductType = new OptionSetValue();
opProductType = (OptionSetValue)item.Attributes[attributeName];
var optionValue = opProductType.Value;
第二:
var StatusString = TermAndCon.FormattedValues[attributeName].ToString();
(设置)后选项设置值:
newSalesOrder[attributeName] = new OptionSetValue(Convert.ToInt32(optionValue));