How to get parameter set from field in object in CRM 2011 using crm sdk and C #

How to get parameter set from field in object in CRM 2011 using crm sdk and C #? I just want to share with you guys a direct approach to getting a set of field options in essence.

+4
source share
5 answers

The right way to get metadata information in Dynamics CRM is to get only the information you need. We should only get the values ​​specified by the parameter based on the original question. Retrieving all metadata for an object when all requirements are specified, these values ​​are not needed for a set of options and will create unnecessary overhead.

.

    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();


    }
+11

, , 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

, , , .

+1

, ?

OptionSetValue CountryOptionSet = Contact.Attributes.Contains("gr_address2_country") ? Contact["gr_address2_country"] as OptionSetValue : null;
 if (CountryOptionSet != null)
                     string Country = CountryOptionSet.Value.ToString();
0

. , :

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<OptionMetadata> OptionsList = (from o in options.Options
                                             where o.Value.Value == optionSetValue
                                             select o).ToList();
        string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
        return optionsetLabel;

I got this from Guido on another similar issue, but rcados lacks a third parameter. To get the text you just installed

var foo = GetoptionsetText("lead", "picklist", 1234 , service)

if you have already declared IorganizationService

0
source

There are two ways to get OptionSet text:

Firstly:

OptionSetValue opProductType = new OptionSetValue();
opProductType = (OptionSetValue)item.Attributes[attributeName];
var optionValue = opProductType.Value;

Secondly:

var StatusString = TermAndCon.FormattedValues[attributeName].ToString();

(Set) Post OptionSet Value:

newSalesOrder[attributeName] = new OptionSetValue(Convert.ToInt32(optionValue));
0
source

Source: https://habr.com/ru/post/1538271/


All Articles