如何使用crm sdk和C#从CRM 2011中的实体字段中获取选项集
如何使用crm sdk和C#从CRM 2011中的实体字段中获取选项集? 我只想与您分享一个直接的方法来获取实体中的字段选项集。
在Dynamics CRM中检索元数据信息的正确方法是仅检索所需的信息。 我们应该只根据原始问题检索选项集值。 当所有需求指定的是选项集的值时,检索实体的所有元数据是不必要的,并且将产生不必要的开销。
以下是获取选项集选项列表的正确方法。
public static void GetOptionSet(string entityName, string fieldName, IOrganizationService service) { var attReq = new RetrieveAttributeRequest(); attReq.EntityLogicalName = entityName; attReq.LogicalName = fieldName; attReq.RetrieveAsIfPublished = true; var attResponse = (RetrieveAttributeResponse)service.Execute(attReq); var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata; var optionList = (from o in attMetadata.OptionSet.Options select new {Value = o.Value, Text = o.Label.UserLocalizedLabel.Label}).ToList(); }
此方法需要实体名称,包含选项集的字段名称以及实例化的IOrganizationService。
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Metadata; public void GetOptionSet(string entityName, string fieldName, IOrganizationService service) { RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest(); retrieveDetails.EntityFilters = EntityFilters.All; retrieveDetails.LogicalName = entityName; RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails); EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, fieldName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata; OptionSetMetadata options = picklistMetadata.OptionSet; var optionlist = (from o in options.Options select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList(); //from here you can do anything you want now with the optionlist }
参考:
http://guruprasadcrm.blogspot.ae/2011/12/retrieve-optionset-text-in-crm-2011.html
我希望这能帮助你们中的一些人完成你的项目。
嘿为什么不用这个?
OptionSetValue CountryOptionSet = Contact.Attributes.Contains("gr_address2_country") ? Contact["gr_address2_country"] as OptionSetValue : null; if (CountryOptionSet != null) string Country = CountryOptionSet.Value.ToString();
取决于它是本地的还是全球的。 如果它是本地的那么:
string optionsetText = entity.FormattedValues["new_optionset"];
如果它是全局的那么你需要更多的代码:
public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service) { string AttributeName = attributeName; string EntityLogicalName = entityName; RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest { EntityFilters = EntityFilters.All, LogicalName = EntityLogicalName }; RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails); Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata; Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet; IList OptionsList = (from o in options.Options where o.Value.Value == optionSetValue select o).ToList(); string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label; return optionsetLabel;
我从Guido得到了另一个类似的问题,但rcados缺少第三个参数。 获取刚刚设置的文本
var foo = GetoptionsetText("lead", "picklist", 1234 , service)
假设您已经声明了IorganizationService
获取OptionSet文本有两种方法:
第一:
OptionSetValue opProductType = new OptionSetValue(); opProductType = (OptionSetValue)item.Attributes[attributeName]; var optionValue = opProductType.Value;
第二:
var StatusString = TermAndCon.FormattedValues[attributeName].ToString();
(设置)发布OptionSet值:
上述就是C#学习教程:如何使用crm sdk和C#从CRM 2011中的实体字段中获取选项集分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)
newSalesOrder[attributeName] = new OptionSetValue(Convert.ToInt32(optionValue));
本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ssfiction.com/ckf/1031435.html