2013-04-29 64 views
2

有沒有辦法以編程方式訪問標籤&已在MS CRM Dynamics中創建爲自定義字段的值字段?Dynamics CRM - 訪問自定義產品選項值

我已經加入所謂的「new_producttypesubcode」的,例如,有2個選項的自定義字段,獎盃= 1000000和工具包= 10000001.

我寫的導入實用程序,反映了客戶的網站和他們之間的產品CRM和我想獲得CRM中所有可能的產品選項列表,以查看它們是否在網站中匹配。

因此,在本質上我想...

  1. 獲得可能new_producttypesubcodes及其相應值的列表。
  2. 遍歷網站中的產品變體。
  3. 如果產品變形名稱new_producttypecodes列表中的任何名稱匹配,則添加值百萬

所以,如果我找到一個產品添加到網站及其標記爲「戰利品」和「戰利品」存在於CRM,然後新OptionSetValue(100000001)

我希望是有道理的......

感謝

+0

的optionset是全球性的還是本地的?全局選項集的 – 2013-04-29 13:14:13

回答

5

該函數檢索本地化爲當前用戶的可能值的字典。取自: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; 
} 
+0

非常感謝你 - 今天將通過這項工作 – 2013-04-30 09:13:40

2

是,將數據全部存放在一個屬性(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)。

+2

必須使用RetrieveOptionSetRequest類,對於本​​地選項集,還有可以使用的RetrieveAttributeRequest類。另一件事,你在代碼中只檢查一種語言,最好是通過LocalizedLabels屬性檢查所有標籤。 – 2013-04-29 14:23:41

+1

RetrieveAttributeRequest會更合適。我忘了那個。不過,我只是驗證了具有全局選項集的屬性在上面的代碼中起作用。 – 2013-04-29 14:52:29

+0

是的,你是對的,RetrieveOptionSetRequest對於全局選項集不是強制性的,對我的不準確性抱歉,是因爲我總是使用該類作爲全局選項集。 – 2013-04-29 14:56:26