Showing posts with label CrmSvcUtil. Show all posts
Showing posts with label CrmSvcUtil. Show all posts

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.