locked
CRM 2011 CrmSvcUtil.exe: Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'int' RRS feed

  • Question

  • Hi,

    i use crmsvcutil.exe to generate early bound classes and it's working fine for shared OptionSets, but if the OptionSet is not shared like producttypecode in the product entity then it's trying to convert an OptionSetValue to int and this is not working. In my example i have a not shared OptionSet prg_teilnehmertyp it's easily fixable to just replace

    this.SetAttributeValue("prg_teilnehmertyp", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value))));

    with

                    this.OnPropertyChanging("prg_teilnehmertyp");
                    this.SetAttributeValue("prg_teilnehmertyp", value);
                    this.OnPropertyChanged("prg_teilnehmertyp");

    Any Ideas how to solve this on CrmSvcUtil.exe side?

    To filter the result from CrmSvcUtil.exe i use ICodeWriterFilterService.

    Code snippet from a generated class with CrmSvcUtil.exe

    [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("prg_teilnehmertyp")]
    public Microsoft.Xrm.Sdk.OptionSetValue prg_teilnehmertyp
    {
    get
    {
    Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("prg_teilnehmertyp");
    if ((optionSet != null))
    {
    return ((Microsoft.Xrm.Sdk.OptionSetValue)(System.Enum.ToObject(typeof(Microsoft.Xrm.Sdk.OptionSetValue), optionSet.Value)));
    }
    else
    {
    return null;
    }
    }
    set
    {
    this.OnPropertyChanging("prg_teilnehmertyp");
    if ((value == null))
    {
    this.SetAttributeValue("prg_teilnehmertyp", null);
    }
    else
    {
    this.SetAttributeValue("prg_teilnehmertyp", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value)))); // here the compile error occurs: "Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'int'"
    }
    this.OnPropertyChanged("prg_teilnehmertyp");
    }
    }

    Kind Regards

    Markus



    • Edited by Markus Benz Thursday, June 12, 2014 11:29 AM
    Thursday, June 12, 2014 10:26 AM

All replies

  • Hi,

    Try like as below :

    this.SetAttributeValue("prg_teilnehmertyp", new Microsoft.Xrm.Sdk.OptionSetValue(Convert.ToInt16(value)));

    Edit : Otherwise try to re-generate class file using latest CRM SDK. 


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog


    • Edited by _Vikram Thursday, June 12, 2014 11:17 AM
    Thursday, June 12, 2014 11:05 AM
  • Hi Vigram,

    thx for the fast answer, but how i can solve this problem on crmsvcutil.exe side so the generated code is correct?

    Thursday, June 12, 2014 11:30 AM
  • Please check following link :

    It seems some issue with crmsvcutil.exe. Please try with latest crmsvcutil.exe from latest CRM SDK.

    http://stackoverflow.com/questions/8443981/crm-2011-generated-code-using-icodewriterfilterservice-fails-to-build


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    Thursday, June 12, 2014 12:13 PM
  • Still the same problem. I'm using now 5.0.9690.3739. It's from the latest SDK version.

    The Problem only occur if CrmSvcUtil.exe exports the a not shared OptionSet. If i filter all not shared OptionSets out everything is working fine.

    Thursday, June 12, 2014 3:17 PM
  • Does this work for you ?

    int producytypecode = ((Microsoft.Xrm.Sdk.OptionSetValue)(entity.attributes["fieldschemaname"])).Value;

    Thursday, June 12, 2014 7:54 PM
  • Sorry if i did not stated that clearly enough, but my problem is that if i generate c# code with crmsvcutil.exe i get a compile error if i dont sort not shared optionsetvalues out within a plug-in (ICodeWriterFilterService) for crmsvcutil.exe.

    With the newest version  of crmsvcutil.exe the problem still exists :(


    Friday, June 13, 2014 9:52 AM