using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ServiceModel;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
namespace ClassLibrary2
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Retrive All Vlaue From Single Record on which pluging is fired
new_teston test = (new_teston)service.Retrieve(new_teston.EntityLogicalName, context.PrimaryEntityId, new ColumnSet(true));
// “s” Contain OptionSet Value ( s = 1 , 2 , 3 , …)
int s = Convert.ToInt32(((OptionSetValue)test.Attributes[“new_optionset”]).Value);
// Function For Retriving OptionSet Label Through Value of “s”
string ss = GetPickListText(new_teston.EntityLogicalName, “new_optionset”, s, service);
new_testto tes = new new_testto();
tes.new_name = ss;
service.Create(tes);
}
public string GetPickListText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
{
string AttributeName = attributeName;
string EntityLogicalName = entityName;
RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest();
retrieveDetails.EntityFilters = EntityFilters.All;
retrieveDetails.LogicalName = EntityLogicalName;
RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
OptionSetMetadata options = picklistMetadata.OptionSet;
IList<OptionMetadata> picklistOption = (from o in options.Options where o.Value.Value == optionSetValue select o).ToList();
string picklistLabel = (picklistOption.First()).Label.UserLocalizedLabel.Label;
return picklistLabel;
}
}
}
SOURCE : JUST2CODE.IN
Subscribe to our YouTube channel : https://www.youtube.com/user/TheRussell2012