Dynamics Crm: Get metadata for statuscode/statecode mapping

后端 未结 3 888
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 20:03

In Dynamics CRM 2011, on the Incident entity, the \"Status Reason\" optionset (aka statuscode) is related to the \"Status\" optionset (aka statecode)

e.g. see this scree

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-13 20:18

    Here is working code that will output State/Status mapping for a given entity (you just need to provide the orgServiceProxy):

        var dictState = new Dictionary();
        var dictStatus = new Dictionary>();
    
        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());
            }
    
            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}");
            }
        }
    

提交回复
热议问题