Dynamics CRM - 访问自定义产品选项值
有没有办法以编程方式访问标签&已在MS CRM Dynamics中创建为自定义字段的值字段?Dynamics CRM - 访问自定义产品选项值
我已经加入所谓的“new_producttypesubcode”的,例如,有2个选项的自定义字段,奖杯= 1000000和工具包= 10000001.
我写的导入实用程序,反映了客户的网站和他们之间的产品CRM和我想获得CRM中所有可能的产品选项列表,以查看它们是否在网站中匹配。
因此,在本质上我想...
- 获得可能new_producttypesubcodes及其相应值的列表。
- 遍历网站中的产品变体。
- 如果产品变形名称new_producttypecodes列表中的任何名称匹配,则添加值百万
所以,如果我找到一个产品添加到网站及其标记为“战利品”和“战利品”存在于CRM,然后新OptionSetValue(100000001)
我希望是有道理的......
感谢
该函数检索本地化为当前用户的可能值的字典。取自:CRM 2011 Programatically Finding the Values of Picklists, Optionsets, Statecode, Statuscode and Boolean (Two Options)。
static Dictionary<String, int> GetNumericValues(IOrganizationService service, String entity, String attribute)
{
RetrieveAttributeRequest request = new RetrieveAttributeRequest
{
EntityLogicalName = entity,
LogicalName = attribute,
RetrieveAsIfPublished = true
};
RetrieveAttributeResponse response = (RetrieveAttributeResponse)service.Execute(request);
switch (response.AttributeMetadata.AttributeType)
{
case AttributeTypeCode.Picklist:
case AttributeTypeCode.State:
case AttributeTypeCode.Status:
return ((EnumAttributeMetadata)response.AttributeMetadata).OptionSet.Options
.ToDictionary(key => key.Label.UserLocalizedLabel.Label, option => option.Value.Value);
case AttributeTypeCode.Boolean:
Dictionary<String, int> values = new Dictionary<String, int>();
BooleanOptionSetMetadata metaData = ((BooleanAttributeMetadata)response.AttributeMetadata).OptionSet;
values[metaData.TrueOption.Label.UserLocalizedLabel.Label] = metaData.TrueOption.Value.Value;
values[metaData.FalseOption.Label.UserLocalizedLabel.Label] = metaData.FalseOption.Value.Value;
return values;
default:
throw new ArgumentOutOfRangeException();
}
}
所以,你会然后需要做的是这样的:
Dictionary<String, int> values = GetNumericValues(proxy, "your_entity", "new_producttypesubcode");
if(values.ContainsKey("Trophy"))
{
//Do something with the value
OptionSetValue optionSetValue = values["Trophy"];
int value = optionSetValue.Value;
}
非常感谢你 - 今天将通过这项工作 – 2013-04-30 09:13:40
是,将数据全部存放在一个属性(SDK article)的元数据。您必须检索实体的实体元数据,然后在列表中找到该属性。然后将该属性转换为PicklistAttributeMetadata对象,它将包含一个选项列表。我会提到,通常从CRM中检索元数据是一项昂贵的操作,因此请考虑缓存。
private static OptionSetMetadata RetrieveOptionSet(IOrganizationService orgService,
string entityName, string attributeName)
{
var entityResponse = (RetrieveEntityResponse)orgService.Execute(
new RetrieveEntityRequest
{ LogicalName = entityName, EntityFilters = EntityFilters.Attributes });
var entityMetadata = entityResponse.EntityMetadata;
for (int i = 0; i < entityMetadata.Attributes.Length; i++)
{
if (attributeName.Equals(entityMetadata.Attributes[i].LogicalName))
{
if (entityMetadata.Attributes[i].AttributeType.Value ==
AttributeTypeCode.Picklist)
{
var attributeMD = (PicklistAttributeMetadata)
entityMetadata.Attributes[i];
return attributeMD.OptionSet;
}
}
}
return null;
}
以下是如何使用上述调用将选项写入控制台的方法。
var optionSetMD = RetrieveOptionSet(orgService, "account", "accountcategorycode");
var options = optionSetMD.Options;
for (int i = 0; i < options.Count; i++)
{
Console.WriteLine("Local Label: {0}. Value: {1}",
options[i].Label.UserLocalizedLabel.Label,
options[i].Value.HasValue ? options[i].Value.Value.ToString() : "null");
}
我相信这个工程的全局选项设置属性为好,但如果你知道它是一个全球性的选项设置有它不同的消息可能会多一点效率(SDK article)。
必须使用RetrieveOptionSetRequest类,对于本地选项集,还有可以使用的RetrieveAttributeRequest类。另一件事,你在代码中只检查一种语言,最好是通过LocalizedLabels属性检查所有标签。 – 2013-04-29 14:23:41
RetrieveAttributeRequest会更合适。我忘了那个。不过,我只是验证了具有全局选项集的属性在上面的代码中起作用。 – 2013-04-29 14:52:29
是的,你是对的,RetrieveOptionSetRequest对于全局选项集不是强制性的,对我的不准确性抱歉,是因为我总是使用该类作为全局选项集。 – 2013-04-29 14:56:26
的optionset是全球性的还是本地的?全局选项集的 – 2013-04-29 13:14:13