CRM 2011 – How to set OptionSetValue in a plugin

I was creating a new entity and trying to fill in the default values for it and then use the

serviceProxy.Create method to add the new entity.

one of the entities had an OptionSet as a business required.   I tried searching for this but couldn’t find much and in the end I managed to work it out myself.

When you set an OptionSetValue you need to create a new one.  One of the problems I had was I because I was using Early Binding the values are set as null, so I couldn’t work out what it should be by looking at an example.

OptionSetValue option = new OptionSetValue(143570000);

The important thing you need to consider is the value is the value on the optionSet and its not very likely to be 1, 2, 3.  If you have created the optionSet yourself the default values are 140000000 or something huge like that.

I had to look on the form to find the value but then once you have made the new option set you can either add it directly to the entity

newCan.hosk_ContactPreference = option;

or if you are using the Entity you need to add it as an attribute

Entity ent = new Entity();

ent.Attributes.Add(“hosk_contactpreference”, option);

3 thoughts on “CRM 2011 – How to set OptionSetValue in a plugin

  1. Balu Chandran May 5, 2011 / 6:32 pm

    please use like:

    entity.Attributes[“”] = new OptionSetValue();

    Like

  2. Balu Chandran May 5, 2011 / 6:32 pm

    Please use Like:

    ChassisCard.Attributes[“AttributeName”] = new OptionSetValue(INDEX);

    Like

  3. NetArch2010 April 26, 2012 / 2:53 pm

    thanks Hosk and Balu Chandran for posting!

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.