当前位置: 首页 > 编程语言 > C#

如何使用crm sdk和C#从CRM 2011中的实体字段中获取选项集分享

时间:2023-04-11 10:40:55 C#

C#学习教程:CRM2011中如何使用crmsdk和C#获取实体字段的选项集从CRM2011中的实体字段获取选项集?我只是想与您分享一个简单的方法来获取实体中设置的字段选项。在DynamicsCRM中检索元数据信息的正确方法是仅检索您需要的信息。我们应该只检索基于原始问题的选项集值。当所有需求都指定选项集的值时,检索实体的所有元数据是不必要的,并且会产生不必要的开销。这是获取选项集选项列表的正确方法。publicstaticvoidGetOptionSet(stringentityName,stringfieldName,IOrganizationServiceservice){varattReq=newRetrieveAttributeRequest();attReq.EntityLogicalName=entityName;attReq.LogicalName=fieldserviceName;attReq.RetrieveAsIfPublished=true;(attReq);varattMetadata=(EnumAttributeMetadata)attResponse.AttributeMetadata;varoptionList=(fromoinattMetadata.OptionSet.Optionsselectnew{Value=o.Value,Text=o.Label.UserLocalizedLabel.Label}).ToList();此方法需要实体名称、包含选项集的字段名称以及实例化的IOrganizationService。使用Microsoft.Xrm.Sdk;使用Microsoft.Xrm.Sdk.Messages;使用Microsoft.Xrm.Sdk.Metadata;publicvoidGetOptionSet(stringentityName,stringfieldName,IOrganizationServiceservice){RetrieveEntityRequestretrieveDetails=newRetrieveEntityRequest();retrieveDetails.EntityFilters=EntityFilters.All;retrieveDetails.LogicalName=entityName;检索实体响应retrieveEntityResponseObj=(RetrieveEntityResponse)service.Execute(retrieveDetails);EntityMetadata元数据=retrieveEntityResponseObj.EntityMetadata;PicklistAttributeMetadatapicklistMetadata=metadata.Attributes.FirstOrDefault(attribute=>String.Equals(attribute.LogicalName,fieldName,StringComparison.OrdinalIgnoreCase))asPicklistAttributeMetadata;OptionSetMetadata选项=picklistMetadata.OptionSet;varoptionlist=(fromoinoptions.Optionsselectnew{Value=o.Value,Text=o.Label.UserLocalizedLabel.Label}).ToList();//从这里你可以做任何你想做的事现在有了选项列表}参考:http://guruprasadcrm.blogspot.ae/2011/12/retrieve-optionset-text-in-crm-2011.html我希望这对你们中的一些人的项目有所帮助嘿为什么不使用这?OptionSetValueCountryOptionSet=Contact.Attributes.Contains("gr_address2_country")?联系方式["gr_address2_country"]作为OptionSetValue:null;如果(CountryOptionSet!=null)stringCountry=CountryOptionSet.Value.ToString();全球的。如果它是本地的,那么:stringoptionsetText=entity.FormattedValues["new_optionset"];如果它是全局的,那么你需要更多的代码:publicstaticstringGetoptionsetText(stringentityName,stringattributeName,intoptionSetValue,IOrganizationServiceservice){字符串EntityLogicalName=entityName;RetrieveEntityRequestretrieveDetails=newRetrieveEntityRequest{EntityFilters=EntityFilters.All,LogicalName=EntityLogicalName};检索实体响应retrieveEntityResponseObj=(RetrieveEntityResponse)service.Execute(retrieveDetails);Microsoft.Xrm.Sdk.Metadata.EntityMetadata元数据=retrieveEntityResponseObj.EntityMetadata;Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadatapicklistMetadata=metadata.Attributes.FirstOrDefault(attribute=>String.Equals(attribute.LogicalName,attributeName,StringComparison.OrdinalIgnoreCase))asMicrosoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;Microsoft.Xrm.Sdk.Metadata.OptionSetMetadataoptions=picklistMetadata.OptionSet;IListOptionsList=(fromoinoptions.Optionswhereo.Value.Value==optionSetValueselecto).ToList();字符串optionsetLabel=(OptionsList.First()).Label.UserLocalizedLabel.Label;返回选项集标签;我从Guido那里得到了另一个类似的问题,但是rcados缺少第三个参数Getthetextyoujustsetvarfoo=GetoptionsetText("lead","picklist",1234,service)假设你已经声明了IorganizationServicegetOptionSettext有两种方法:首先:OptionSetValueopProductType=newOptionSetValue();opProductType=(OptionSetValue)item.Attributes[attributeName];varoptionValue=opProductType.Value;第二:varStatusString=TermAndCon.FormattedValues[attributeName]。字符串();(设置)发布OptionSet值:以上是C#学习教程:CRM2011中如何使用crmsdk和C#从实体字段中获取optionset共享的全部内容,如果对大家有用需要了解一下更多关于C#学习教程,希望大家多多关注—newSalesOrder[attributeName]=newOptionSetValue(Convert.ToInt32(optionValue));如需转载请注明出处:

最新推荐
猜你喜欢