Dynamics Crm:获取状态码/状态码映射的元数据

时间:2013-03-17 17:33:51

标签: c# metadata dynamics-crm-2011

在Dynamics CRM 2011中,在Incident实体上,“Status Reason”选项集(aka statuscode)与“Status”选项集(aka statecode)相关

e.g。看这个截图

screenshot of CRM field options

当我使用API​​检索“状态原因”选项集时,如下所示:

        RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
        {
            EntityLogicalName = "incident",
            LogicalName = "statuscode",
            RetrieveAsIfPublished = true
        };
        RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)serv.Execute(attributeRequest);
        AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
        StatusAttributeMetadata statusMetadata = (StatusAttributeMetadata)attrMetadata;
        var dict = new Dictionary<int?, string>();
        foreach (OptionMetadata optionMeta in statusMetadata.OptionSet.Options)
        {
            dict.Add(optionMeta.Value, optionMeta.Label.UserLocalizedLabel.Label);
        }

它的工作原理是我获得了“状态原因”(statuscode)选项的完整列表。但是,我没有得到任何关于“状态原因”(状态代码)选项与哪个“状态”(状态代码)选项相关的信息。

如何获取该信息?

3 个答案:

答案 0 :(得分:12)

您已经拥有了所有内容,请尝试在foreach中插入此代码:

 int stateOptionValue = (int)((StatusOptionMetadata)optionMeta).State;

如果使用StatusOptionMetadata的State属性,请参见StatusAttributeMetaData.OptionSet.Options层次结构可以返回名为StatusOptionMetadata的类型,它将返回此状态码所属的状态码。

答案 1 :(得分:4)

以下是通过查询数据库来获取它的方法

SELECT distinct e.LogicalName as entity,
      smState.Value AS stateCode,
      smstate.AttributeValue,
      smStatus.Value AS [statuscode/statusreason],
      smStatus.AttributeValue
FROM StatusMap sm
     JOIN Entity e ON sm.ObjectTypeCode = e.ObjectTypeCode
     JOIN StringMap smState ON smState.AttributeValue = sm.State
                           AND smState.ObjectTypeCode = e.ObjectTypeCode
                           AND smState.AttributeName = 'StateCode'
     JOIN StringMap smStatus ON smStatus.AttributeValue = sm.Status
                            AND smStatus.ObjectTypeCode = e.ObjectTypeCode
                            AND smStatus.AttributeName = 'StatusCode'
WHERE  e.LogicalName in ('lead')
ORDER BY e.LogicalName,
      smState.AttributeValue,
      smStatus.AttributeValue;

答案 2 :(得分:1)

以下是输出给定实体的状态/状态映射的工作代码(您只需要提供orgServiceProxy):

    var dictState = new Dictionary<int, OptionMetadata>();
    var dictStatus = new Dictionary<int, List<OptionMetadata>>();

    string entityName = "lead";
    int count=0;
    using (var orgServiceProxy = GetOrgServiceProxy(orgServiceUriOnLine))
    {
        RetrieveAttributeResponse attributeResponse = GetAttributeData(orgServiceProxy, entityName, "statecode");
        AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
        StateAttributeMetadata stateMetadata = (StateAttributeMetadata)attrMetadata;
        foreach (OptionMetadata optionMeta in stateMetadata.OptionSet.Options)
        {
            dictState.Add(optionMeta.Value.Value,optionMeta);
            dictStatus.Add(optionMeta.Value.Value,new List<OptionMetadata>());
        }

        attributeResponse = GetAttributeData(orgServiceProxy, entityName, "statuscode");
        attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
        StatusAttributeMetadata statusMetadata = (StatusAttributeMetadata)attrMetadata;

        foreach (OptionMetadata optionMeta in statusMetadata.OptionSet.Options)
        {
            int stateOptionValue = ((StatusOptionMetadata)optionMeta).State.Value;
            var statusList = dictStatus[stateOptionValue];
            statusList.Add(optionMeta);
            count++;
        }
    }
    Console.WriteLine($"Number of mappings: {count}");
    foreach (var stateKvp in dictState.OrderBy(x=> x.Key))
    {
        Console.WriteLine($"State: {stateKvp.Value.Value}: {stateKvp.Value.Label.UserLocalizedLabel.Label}");
        var statusList = dictStatus[stateKvp.Key];
        Console.WriteLine($"\tStatuses");
        foreach (var status in statusList.OrderBy(s => s.Value))
        {
            Console.WriteLine($"\t\t{stateKvp.Value.Value}: {status.Label.UserLocalizedLabel.Label}");
        }
    }