Thursday, December 22, 2011

How to get OptionSet value inside a plugin

 OptionSetValue oValue = (OptionSetValue)entity.Attributes["new_myentity"];
                        int optionSetValue = oValue.Value;
 var pOptionMetadata = optionList.Where(c => c.Value == optionSetValue).FirstOrDefault();
 string myOptionSetText=pOptionMetadata .Label.LocalizedLabels[0].Label

    public OptionMetadata[] GetOptionMetadata(string logicalEntityName, string optionSetAttributeName, IOrganizationService service)
        {
            RetrieveAttributeRequest retrieveAttributeRequest =
                                         new RetrieveAttributeRequest
                                         {
                                             EntityLogicalName = logicalEntityName,
                                             LogicalName = optionSetAttributeName,
                                             RetrieveAsIfPublished = true
                                         };

            // Execute the request.
            RetrieveAttributeResponse retrieveAttributeResponse =
                (RetrieveAttributeResponse)service.Execute(
                retrieveAttributeRequest);

            // Access the retrieved attribute.
            PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
                (PicklistAttributeMetadata)
                retrieveAttributeResponse.AttributeMetadata;

            // Get the current options list for the retrieved attribute.
            OptionMetadata[] optionList =
                retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

            return optionList;
        }

2 comments:

  1. Hello Atif good blog.
    I would suggest you to use SyntaxHighlighter for better code readability.

    Regards,
    Yusuf

    ReplyDelete
  2. Hello Atif good article .Thanks for sharing

    I have one question,for the same code I need to add date format

    Eg CAH-00000-20180212

    CAH-{seq:5}-{date:yyyymmdd}

    How to do that and where to change the above plugin code .. much appreciated can help on the this

    ReplyDelete