1

I'm creating new Entity records in C#. The problem is my early-bound Xrm class is expecting the integer value of the Option List in question, but all I have is the string value of the Option List.

So, this is what I'd like to do. The problem is the "OptionListValue" in question is the integer value. You know; The auto-created one that's huge.

Is the only way for me to do this by finding out the value of that particular option? If so, what API do I use to get it and how do I use it? I'm expecting there's some Linq method of doing so. But I'm possibly assuming too much.

public void CreateNewContactWithOptionListValue(string lastName, string theOptionListValue)
{
    using ( var context = new CrmOrganizationServiceContext( new CrmConnection( "Xrm" ) ) )
    {
        var contact = new Contact()
        {
            LastName = lastName,
            OptionListValue = theOptionListValue // How do I get the proper integer value from the CRM?
        };
        context.Create( contact );
    }
}
IAmAN00B
  • 1,913
  • 6
  • 27
  • 38
  • 1
    check this answer http://stackoverflow.com/a/16280780/2191473 it explains how to retrieve the optionset value. – Guido Preite Jun 06 '13 at 20:22
  • possible duplicate of [Dynamics CRM - Accessing Custom Product Option Value](http://stackoverflow.com/questions/16279214/dynamics-crm-accessing-custom-product-option-value) – James Wood Jun 07 '13 at 07:58

1 Answers1

0

Method to do it without using web service:

  1. generate enums for option sets (here is how you can do it)
  2. once you have enum, just parse string value. Something like this:
    public void CreateNewContactWithOptionListValue(string lastName, string theOptionListValue)
    {
        using (var context = new CrmOrganizationServiceContext(new CrmConnection("Xrm")))
        {
            new_customoptionset parsedValue;
            if (!Enum.TryParse<new_customoptionset>(theOptionListValue, out parsedValue))
            {
                throw new InvalidPluginExecutionException("Unknown value");
            }
            var contact = new Contact()
            {
                LastName = lastName,
                OptionListValue = new OptionSetValue((int)parsedValue)
            };
            context.Create(contact);
        }
    }

Be careful with space in option labels, as they are removed in enums

MarioZG
  • 2,087
  • 11
  • 19