Monday, March 12, 2012

How to use CrmSvcUtil (Improved Version) in Visual Studio Part 2

Few days ago, I posted a blog on how to use CrmSvcUtil in Visual Studio. Here is link to that blog. Last few days, I spent some time on working out how to extend the CrmSvcUtil. I read this article on MSDN and I figured out that my last blog was not the best practice solution to use the CrmSvcUtil in Visual Studio. Here is new improved version of “How to use CrmSvcUtil in Visual Studio”
  1. In Visual Studio, create a new “Class Library” project as shown in the following screen shot csunew_thumb[4]
  2. Delete the class file “class1.cs” created by the Visual Studio.
  3. Add the references to following files to the project. These files can be find in SDK\Bin folder of CRM SDK. You can add a reference by right-clicking the name of your project and then selecting Add Reference.
    • CrmSvcUtil.exe
    • Microsoft.Crm.Sdk.Proxy.dll (Only needed for the on-premise and IFD installation)
    • Microsoft.Xrm.Sdk.dll
  4. Click on “Show All Files” in solution explorer as shown in the following screen shot.csu8_thumb[6]
  5. The solution explorer will look like the following screen shot.csu9_thumb[6]
  6. Click on “bin” folder highlighted in yellow in the above screen shot. It will open open up an another folder “debug”.Click on the debug folder.Now the solution explorer will look like the following screen shot.csu10_thumb[7]
  7. Now we need to add CrmSvcUtil.exe.config to the debug folder. There are following two ways to achieve this.
    • Add the file to debug folder by using window explorer outside the visual studio.
    • or Right Click on the bin folder in the solution explorer and select “Include in project”. Now Select the debug folder and add an application configuration file by selecting “Project” menu----“Add New Item”---“Application Configuration File”.
  8. CrmSvcUtil.exe.config will contain “CrmSvcUtil.exe parameters”. The solution explorer will look like a following screen shot.csu11_thumb[3]

  9. Here is a list of all the parameters we can use with CrmSvcUtil.
  10. Add the following keys to CrmSvcUtil.exe.config file.
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="url" value="https://cdc.crm5.dynamics.com/XRMServices/2011/Organization.svc"/>
        <add key="o" value="CrmProxy.cs"/>
        <add key="u" value="username@live.com"/>
        <add key="p" value="password"/>
        <add key="servicecontextname" value="XrmContext"/>   
      </appSettings>
    </configuration>
  11. Now the interesting part, right click on project node and press properties.csu5_thumb3
  12. It will pop up a following dialog box. Click on the “Build Events” tab.csu12 
  13. Type "$(TargetDir)CrmSvcUtil.exe" in “Post-build event command line” and choose “On Successful build” on  “Run the post-build event” as shown in the above screen shot.
  14. Now Build the solution.
  15. You can check the “Output Window” of Visual Studio for the result. If everything goes smoothly, it will create “CrmProxy.cs” file in debug folder.
  16. Click on “Refresh” button in solution explorer and you can see the file in the debug directory.
  17. Check the CrmProxy.cs, it will have all the crm entities classes and "XrmContext".

Tips


  • 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
  • <add key="o" value="CrmProxy.cs"/> entry in the config file to generate the output file in the desired folder. for e.g <add key="o" value="C:\Users\amreek\Desktop\CrmProxy.cs"/>

Note

    I have chosen to run the CrmSvcUtil.exe in Post-build event. I will explain the reason in my next blog.

19 comments:

  1. You need to run this only if there are changes in the schema , not every build !!

    ReplyDelete
    Replies
    1. Thanks for the feed back budddy. I m just suggesting an approach. You can use it the way you would like to use it.
      I am going to add this project to all of crm projects and run this for every project just by changing the values in the config file.

      Delete
  2. This worked like a charm thanks! a had a little problem with the post-build command line, because my TargetDir had spaces in it. I ended up putting the director location in quotes as someone suggest.. like "c:\vs 2010\projects\bin\Debug\CrmSvcUtil.exe". The build worked after that.

    ReplyDelete
  3. can you please send the complete "post-build event command line" please ?

    I have tried putting in quotes but didnt work foe me.

    ReplyDelete
  4. I have follow the above steps , but step 6 does not show the files in bin - debug folder. whay is that?

    ReplyDelete
    Replies
    1. I encountered the same problem and I got around it by adding CrmSvcUtil.exe and Microsoft.Xrm.Sdk.dll to the debug folder using the Add -> Existing Item functionality

      Delete
  5. If you have added the references in step 3. these files should be there.

    Regards..

    ReplyDelete
  6. I have reopen the project again I can see the files now, in step 10 URL username and password do I need to change these?

    ReplyDelete
    Replies
    1. Yes, you should use your CRM credentials.

      Regards

      Delete
  7. I'm getting an "Exited with Code 1" and it spits out the standard help text.

    It seems that it's not paying attention to the Config file.

    I've tried both CrmSvcUtil.config and App.config.

    I'm running VS 2012 and on premise CRM 2011.

    Any thoughts?

    ReplyDelete
  8. Hi I am so grateful I found your webpage, I really
    found you by mistake, while I was looking on Aol for something else, Nonetheless I am here now and would
    just like to say many thanks for a remarkable post and a all
    round exciting blog (I also love the theme/design), I don't have time to look over it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the excellent job.

    Also visit my page Chaussure De Foot

    ReplyDelete
  9. You ROCK!

    I can't tell you how much I appreciate this -- will save me just buckets of time!

    Great post!

    ReplyDelete
  10. I have a solution with 3 distinct projects, each with a different namespace. I want to generate 3 different files, each with different parameters. is this possible with this method?

    ReplyDelete
    Replies
    1. In case it wasn't clear, I want to make all 3 servicecontext files with 1 project in 1 build.

      Delete
  11. Error 1 The command "c:\users\username\documents\visual studio 2012\Projects\OnPremTest\OnPremTest\bin\Debug\Crm.exe" exited with code 9009. Please let me know the solution

    ReplyDelete
  12. Your solution just saved me, i have been on these for over a month, i was using the command prompt but it didn't work. Thanks a lot

    ReplyDelete