- In Visual Studio, create a new “Class Library” project as shown in the following screen shot
- Delete the class file “class1.cs” created by the Visual Studio.
- 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.
- Microsoft.Crm.Sdk.Proxy.dll (Only needed for the on-premise and IFD installation)
- Click on “Show All Files” in solution explorer as shown in the following screen shot.
- The solution explorer will look like the following screen shot.
- 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.
- 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”.
- CrmSvcUtil.exe.config will contain “CrmSvcUtil.exe parameters”. The solution explorer will look like a following screen shot.
- Here is a list of all the parameters we can use with CrmSvcUtil.
- 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="firstname.lastname@example.org"/> <add key="p" value="password"/> <add key="servicecontextname" value="XrmContext"/> </appSettings> </configuration>
- Now the interesting part, right click on project node and press properties.
- It will pop up a following dialog box. Click on the “Build Events” tab.
- 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.
- Now Build the solution.
- 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.
- Click on “Refresh” button in solution explorer and you can see the file in the debug directory.
- 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 <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"/>
I have chosen to run the CrmSvcUtil.exe in Post-build event. I will explain the reason in my next blog.