Pages

Thursday 21 November 2013


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;
            }
        }
    }

Tuesday 12 November 2013

Plug-in

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using System.Runtime.Serialization;

namespace student
{
    public class students: IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            Entity targetEntity=null;
            if(context.InputParameters.Contains("Target")&&context.InputParameters["Target"]is Entity)
            {
                targetEntity = (Entity)context.InputParameters["Target"];
                if(targetEntity.LogicalName!="new_selectedapplicant")
                {
                    return;
                }
            }
            else
            {
                return;
            }
            try
            {
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                string a = targetEntity.FormattedValues["new_groupselected"];
           
   
             


                string b = ((string)targetEntity["new_name"]);
                string c = ((string)targetEntity["new_lastname"]);
                string d = ((string)targetEntity["new_fathersname"]);
                string e = ((string)targetEntity["new_emailid"]);
                string f = ((string)targetEntity["new_schoolname"]);
                decimal g = ((decimal)targetEntity["new_english"]);
                decimal h = ((decimal)targetEntity["new_hindi"]);
                decimal i = ((decimal)targetEntity["new_science"]);
                decimal j = ((decimal)targetEntity["new_maths"]);
                Entity contact = new Entity();
                if (a=="science")
                {
                    contact.LogicalName = "new_sciencestudent";
                    contact.Attributes.Add("new_name", b);
                    contact.Attributes.Add("new_lastname", c);
                    contact.Attributes.Add("new_fathersname", d);
                    contact.Attributes.Add("new_emailid", e);
                    contact.Attributes.Add("new_schoolname", f);
                    contact.Attributes.Add("new_english", g);
                    contact.Attributes.Add("new_hindi", h);
                    contact.Attributes.Add("new_science", i);
                    contact.Attributes.Add("new_maths", j);
                    service.Create(contact);
                }
                else if (a =="commerce")
                {
                    contact.LogicalName = "new_commercestudent";
                    contact.Attributes.Add("new_name", b);
                    contact.Attributes.Add("new_lastname", c);
                    contact.Attributes.Add("new_fathersname", d);
                    contact.Attributes.Add("new_emailid", e);
                    contact.Attributes.Add("new_schoolname", f);
                    contact.Attributes.Add("new_english", g);
                    contact.Attributes.Add("new_hindi", h);
                    contact.Attributes.Add("new_science", i);
                    contact.Attributes.Add("new_maths", j);
                    service.Create(contact);


                }
                else
                {
                    contact.LogicalName = "new_artsstudent";
                    contact.Attributes.Add("new_name", b);
                    contact.Attributes.Add("new_lastname", c);
                    contact.Attributes.Add("new_fathersname", d);
                    contact.Attributes.Add("new_emailid", e);
                    contact.Attributes.Add("new_schoolname", f);
                    contact.Attributes.Add("new_english", g);
                    contact.Attributes.Add("new_hindi", h);
                    contact.Attributes.Add("new_science", i);
                    contact.Attributes.Add("new_maths", j);
                    service.Create(contact);
                }
            }
            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));

            }
       
       





       
       
        }
    }
}