使用CRM 4.0 webservice设置BusinessEntity选项列表值

时间:2009-11-20 11:43:27

标签: c# web-services dynamics-crm dynamics-crm-4

我正在尝试在CRM 4.0中创建一个ImportMap对象。我需要设置TargetEntity属性,这是一个Picklist值。 link text似乎意味着可以使用

来完成
importmap map = new importmap();
map.name = "test map";
map.targetentity = new Picklist();
map.targetentity.name = "Contact"

但这似乎总是将目标实体属性保留为null。

有什么想法?

2 个答案:

答案 0 :(得分:1)

如果是选择列表,则必须提供索引值。假设你有一个名为TargetValues.Contact的枚举值为1,那么它会像:

map.targetentity = new Picklist();
//map.targetentity.name = "Contact"
map.targetentity.Value = Convert.ToInt32(TargetEntities.Contact);

- 编辑 -

我相信你想要实现的目标,你必须以某种方式检索目标实体,然后才能枚举它们。

See this,这可能会有所帮助。

如果以上是没用的,那么请按以下步骤操作:

  1. 从用户界面打开选项列表,记下索引的数量及其相应的值。
  2. 在代码中创建一个枚举,并为其分配这些值/索引。
  3. 在运行时使用枚举(在上面的示例中给出),以设置选项列表。
  4. 但是某种程度上索引可能会在一段时间内发生变化,因此在这种情况下,您可以使用这些值/索引创建枚举的xml,并在运行时加载它们。但这种方法的问题是:

    1. 每当有人要更改索引时,管理员也必须更改xml文件。
    2. 加载外部xml文件是一种开销。
    3. 不太好的方法,但是在一个滴答作响的截止日期之前,你可以做到这一点;这永远不会破坏或造成麻烦。只是那个xml加载部分不好。

      顺便说一下,如果你要浏览CRM SDK,你会发现类似的例子。

答案 1 :(得分:0)

将此功能用于动态请求选项列表值:

public IDictionary<int,string> PickListValues(string entityname, string attributename)
{
    var req = new RetrieveAttributeRequest();
    req.EntityLogicalName = entityname;
    req.LogicalName = attributename;
    req.RetrieveAsIfPublished = true;

    var response = (RetrieveAttributeResponse)Service.MetaDataService.Execute(req);

    var picklist = (PicklistAttributeMetadata)response.AttributeMetadata;

    var res = new Dictionary<int,string>();

    foreach (var item in picklist.Options)
       res.Add(item.Value.Value, item.Label.UserLocLabel.Label);

    return res;
}