Monday 16 January 2012

Generate code with crmsvcutil #crmsvcutil extension #OrganizationServiceContext #OrganizationServiceContext

Syntax
CrmSvcUtil.exe /url:http:////XRMServices/2011/Organization.svc
/out:.cs /username: /password: /domain:
/namespace: /serviceContextName:

An example for generating early bound entities with a service context
CrmSvcUtil.exe ^
/url:http://localhost:5555/Crm2011RTM/XRMServices/2011/Organization.svc ^
/out:Xrm.cs ^
/language:CS ^
/domain:playground ^
/username:user-defined-usrname ^
/password:user-defined-pwd ^
/namespace:Xrm ^
/servicecontextname:XrmServiceContext

::The following para will only work if you run this bat file in the sdk\bin folder due to required dlls.
::/codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration"

pause

An example for generating optionsets
CrmSvcUtil.exe ^
/codewriterfilter:"Microsoft.Crm.Sdk.Samples.FilteringService, GeneratePicklistEnums" ^
/codecustomization:"Microsoft.Crm.Sdk.Samples.CodeCustomizationService, GeneratePicklistEnums" ^
/namingservice:"Microsoft.Crm.Sdk.Samples.NamingService, GeneratePicklistEnums" ^
/url:http://servername/orgname/XRMServices/2011/Organization.svc ^
/out:OptionSets.cs

pause

* Copied from Crm 2011 sdk

To use the generated code you need using the following assemblies.
//For the on-premises or Internet-Facing Deployment (IFD) versions of Microsoft Dynamics CRM, //browse to the SDK\Bin\ folder.
Microsoft.Crm.Sdk.Proxy.dll

//For Microsoft Dynamics CRM for Outlook Online, browse to the SDK\Bin\Online\ folder.
Microsoft.Xrm.Sdk.dll

CodeGeneration extension VS no extension
Developer Extensions for Microsoft Dynamics CRM 2011 provides an extension to the CrmSvcUtil.exe command-line tool, called the Microsoft.Xrm.Client.CodeGeneration extension, which you can use to generate the data context and data transfer object classes for your Microsoft Dynamics CRM organization.

So what is the difference between use the extension or not?

As you can see from the screen dump below. When the codeCustomization extension is used, event related code is NOT generated therefore less code is generated. So use Microsoft.Xrm.Client.CodeGeneration to generate early bound crm entities are favorable. Just remember to run the bat file in the sdk\bin folder otherwise you may get error due to missing libraries.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi I read your post and its helpful for me, could you please let me know how to pass the option set value for a field.

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

    How to set AccountCategoryCode value?

    ReplyDelete