Wednesday 30 April 2014

Plugin code to retrieve the label from Optionset value

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;


namespace online
{
    public class plugintest : IPlugin
    {
        public void Execution(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            Entity targetEntity = null;
            if(context.InputParameters.Contains("Target")&& context.InputParameters["Target"]is Entity)
            {
                targetEntity = (Entity)context.InputParameters["Target"];
                if(targetEntity.LogicalName!="new_applicant")
                {
                    return;
                }

            }
            else
            {
                return;
            }
            try
            {
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                OptionSetValue a = (OptionSetValue)targetEntity["new_groupselected"];
                string b = Getoptionsetbyvalue(service, "new_groupselected", a);
                targetEntity.Attributes.Add("new_displaylabel", b);
                service.Update(targetEntity);

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException(string.Concat("an error occured in plugin ", ex.Message, Environment.NewLine, ex.StackTrace));
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(string.Concat("An error occured in plugin ", ex.Message, Environment.NewLine, ex.StackTrace));

            }
        }
            public static string Getoptionsetbyvalue(IOrganizationService serviceProvider,string attribute,OptionSetValue option)
            {
                string label=string.Empty;
                RetrieveAttributeRequest retriveattributrequest=new RetrieveAttributeRequest();
                retriveattributrequest.EntityLogicalName="new_selecetedapplicant";
                retriveattributrequest.LogicalName="new_groupselected";
                retriveattributrequest.RetrieveAsIfPublished=true;

                RetrieveAttributeResponse retriveattributeresponse=(RetrieveAttributeResponse)serviceProvider.Execute(retriveattributrequest);
                AttributeMetadata attributemetadat=(AttributeMetadata)retriveattributeresponse.AttributeMetadata;
                PicklistAttributeMetadata picklist=(PicklistAttributeMetadata)attributemetadat;
                foreach(OptionMetadata metadata in picklist.OptionSet.Options)
                {
                    if(metadata.Value==option.Value)
                    {
                        label=metadata.Label.UserLocalizedLabel.Label;

                    }
                }
                return label;
            }
        }
    }

No comments:

Post a Comment