3

Using the sample code at http://erikpool.blogspot.com/2011/03/filtering-generated-entities-with.html I have altered this so that GenerateEntity and GenerateOptionSet to have the code:

return optionSetMetadata.Name.ToLowerInvariant().StartsWith("myprefix");

This generates the types including some enumerations for the optionsets. The actual implementation of the optionset in the entity doesn't use this however, but I get the following:

    [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("myprefix_fieldname")]
    public Microsoft.Xrm.Sdk.OptionSetValue myprefix_FieldName
    {
        get
        {
            Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("myprefix_fieldname");
            if ((optionSet != null))
            {
                return ((Microsoft.Xrm.Sdk.OptionSetValue)(System.Enum.ToObject(typeof(Microsoft.Xrm.Sdk.OptionSetValue), optionSet.Value)));
            }
            else
            {
                return null;
            }
        }
        set
        {
            this.OnPropertyChanging("myprefix_FieldName");
            if ((value == null))
            {
                this.SetAttributeValue("myprefix_fieldname", null);
            }
            else
            {
                this.SetAttributeValue("myprefix_fieldname", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value))));
            }
            this.OnPropertyChanged("myprefix_FieldName");
        }
    }

Obviously casting the OptionSetValue to an int in the setter does not compile, I assume that it should be generating the property with a type that matches the generated enum, but isn't. What do I need to do to correct this?

Duncan Watts
  • 1,331
  • 9
  • 26

1 Answers1

1

It looks like there was a bug in the crmsrvcutil that has since been fixed. My code for OptionSet properties now looks like this:

[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("prioritycode")]
public Microsoft.Xrm.Sdk.OptionSetValue PriorityCode
{
    get
    {
        return this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("prioritycode");
    }
    set
    {
        this.OnPropertyChanging("PriorityCode");
        this.SetAttributeValue("prioritycode", value);
        this.OnPropertyChanged("PriorityCode");
    }
}

And I get no error setting the OptionSetValue...

Daryl
  • 18,592
  • 9
  • 78
  • 145
  • I'm using the latest version crmsvcutil (5.0.9690.3218) and the code generated for my OptionSet properties is still outputting in the problematic form from the original post. :/ – Polshgiant Feb 20 '13 at 18:06
  • @Polshgiant Consider adding your own question. Writing in people's comments is likely not going to result in an answer for your particular question. – Daryl Feb 20 '13 at 20:33
  • I was just noting that my experience with the latest version of crmsvcutil is different from yours. I eventually worked around the problem using a combination of answers from the post Guarav linked to above. – Polshgiant Feb 21 '13 at 05:03