Dynamics CRM中:获取的StatusCode / statecode映射元数据(Dynam

2019-08-17 17:05发布

在动态CRM 2011,对事件的实体,“状态原因” optionset(又名的StatusCode)有关“状态” optionset(又名statecode)

例如,看到这张截图

当我使用API​​来检索状态原因optionset,就像这样:

        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)选项的整个列表它工作的地方。 不过,我不明白哪些“状态原因”(的StatusCode)选项是指其“状态”(statecode)选项的任何信息。

如何获取这些信息?

Answer 1:

你已经拥有了一切尝试插入的foreach这里面的代码:

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

见StatusAttributeMetaData.OptionSet.Options层次可以返回一个称为类型StatusOptionMetadata如果使用StatusOptionMetadata的国家财产,它会返回此的StatusCode属于statecode。



Answer 2:

这里是你如何可以通过查询数据库得到它

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;


Answer 3:

这里是工作的代码,将针对给定的实体输出状态/状态映射(你只需要提供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}");
        }
    }


文章来源: Dynamics Crm: Get metadata for statuscode/statecode mapping