Thursday, March 8, 2012

How to use CrmSvcUtil (Code generation tool) in Visual Studio Part 1

We use CrmSvcUtil to generate early bind classes in CRM2011. Also, we have an option to generate Organisation service context. The only problem is that it is a command line tool. We have to go to command prompt, type the command with required parameters and then move the generated file to our project. It is a bit annoying.
In this blog we will learn, how to use CrmSvcUtil tool in Visual Studio. Here are the steps.
  1. In Visual Studio, create a new “Class Library” project as shown in the following screen shot csu1
  2. Delete the class file “class1.cs” created by the Visual Studio.
  3. Add the following files to the project from SDK\Bin folder of CRM SDK.
    • CrmSvcUtil.exe
    • Microsoft.Crm.Sdk.Proxy.dll
    • Microsoft.Xrm.Sdk.dll
  4. Add an  application configuration file to the project and name it “CrmSvcUtil.exe.config”. This file will contain all the parameters we can pass to “CrmSvcUtil.exe”. The solution explorer will look like a following screen.csu4
  5. Here is a list of all the parameters we can use with CrmSvcUtil.
  6. Add the following keys to CrmSvcUtil.exe.config file.
    <?xml version="1.0" encoding="utf-8" ?>
        <add key="url" value=""/>
        <add key="o" value="CrmProxy.cs"/>
        <add key="u" value=""/>
        <add key="p" value="password"/>
        <add key="servicecontextname" value="XrmContext"/>   
  7. Now the interesting part, right click on project node and press properties.csu5
  8. It will pop up a following dialog box. Click on the “Debug” tab csu6
  9. Select “Start external program” and choose the CrmSvcUtil.exe, we added in step 3.
  10. Now choose the “Working directory” where you want the output file to go.
  11. Debug the code and it will come up with following screen.csu7
  12. You can check the “Output Window” of  Visual Studio for the result. If everything goes smoothly, it will create “CrmProxy.cs” file in the folder selected in “Working directory” in step 14.
  13. Include the “CrmProxy.cs” file into the project.
  14. Check the CrmProxy.cs, it will have all the crm entities classes and "XrmContext".


  • You can add, remove and edit keys in CrmSvcUtil.exe.config to pass parameters to code generator tool.
  • Try accessing the CRM through the browser before debugging, if you are working with CRM Online.
  • You can add this project to any of your crm solution. Change the "Working directory" of the project to generate the CrmProxy file in a desired folder.


  1. I have to design a report using fetchxml, it should contain two parameters, showing user names (manager) repectively. I want that as soon as I select user from first drop down(manager), users under the selected manager should appear in the list..

    Any idea???

  2. Running the project opens a command window which closes when complete. The output window says the program exited with error code 2. Where does the output go?