从CRM Plugin中的OptionSetValue中获取字符串值
本文关键字:获取 字符串 OptionSetValue CRM Plugin 中的 | 更新日期: 2023-09-27 18:13:51
我想知道如何在我正在制作的CRM插件中获得OptionSet的字符串值。我认为我所要做的就是将int值传递给OptionSetValue,但这似乎不起作用。下面是我的代码:
aBillingFrequencyCode = new OptionSetValue(myContract.BillingFrequencyCode.Value).ToString();
但是输出只是
Microsoft.Xrm.Sdk.OptionSetValue
任何想法?
您可以检索OptionSet标签,而无需检索实体的所有元数据。我提供了两种方法。其中一个将使用IOrganizationService运行的帐户的语言代码(LCID)。另一个允许您指定LCID。
注意,如果你打算在代码中广泛使用这些值,你可能想要考虑缓存这个值来提高性能——这将取决于你的特定应用程序的需求。
如果您计划同时检索单个实体上的多个Option Sets的这些值,您应该使用Guido上面的代码,并在一次呼叫中检索所有实体元数据,以减少您需要向CRM进行的呼叫数量。因此,我们的每个代码片段在某些情况下都更有效。
//This method will return the label for the LCID of the account the IOrganizationService is using
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, 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;
return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
}
//This method will return the label for the specified LCID
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, int lcid, 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;
return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.LocalizedLabels.Where(l => l.LanguageCode == lcid).FirstOrDefault().Label;
}
为了获得OptionSet文本值,您需要查询元数据(这是因为Dynamics CRM支持多种语言)
下面是一个例子:
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;
}
考虑到存在entity.FormattedValues["field_name"]
,这将很容易,您只需要识别字段类型并从中获取字符串。
var account = service.Retrieve("account",Guid.Parse(""),new ColumnSet(true));
var statcode = account.Attributes
.Where(a => a.Value.GetType() == typeof(OptionSetValue) && a.Key == "statecode")
.Select(f=> account.FormattedValues[f.Key])
.First();
上面的代码段将识别Filter &从account
实体中获取statecode
文本