Thursday, March 22, 2012

Extending CrmSvcUtil (Filtering the generated classes)

Most people use the CrmSvcUtil  in its native form. But CrmSvcUtil exposes some interfaces to extend its functionality. Here is the list of all the extension interfaces. One of my personal favourite is ICodeWriterFilterService. This interface can control which entity/attribute/relationship etc. would be created by CrmSvcUtil. By default, CrmSvcUtil generates all the entities, attributes and relationships. If you run the CrmSvcUtil for an out of box CRM online organization, it will generate a file of around 5 mb with all the entities and attributes. In reality we need to generate very few entities for our solutions. We do not need all the extra code generated by the utility. The size of the file is even more important when we are writing code for CRM online. In this blog, we are going to learn how to filter the entities generated by the code generation utility.
Here are the steps:
  1. Open up the solution we created in our last blog.
  2. Add a new class file to the project. I named my class file BasicFilteringService.cs. I copied this file from the sdk samples. Here is the code. The code will restrict the code generation utility to generate just the custom entities.
    using System;
    using Microsoft.Crm.Services.Utility;
    using Microsoft.Xrm.Sdk.Metadata;
    namespace CRMExtensions
    {
        /// 
        /// Sample extension for the CrmSvcUtil.exe tool that generates early-bound
        /// classes for custom entities.
        /// 
        public sealed class BasicFilteringService : ICodeWriterFilterService
        {
            public BasicFilteringService(ICodeWriterFilterService defaultService)
            {
                this.DefaultService = defaultService;
            }
    
            private ICodeWriterFilterService DefaultService { get; set; }
    
            bool ICodeWriterFilterService.GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
            {
                return this.DefaultService.GenerateAttribute(attributeMetadata, services);
            }
    
            bool ICodeWriterFilterService.GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
            {
                if (!entityMetadata.IsCustomEntity.GetValueOrDefault()) { return false; }
                
                return this.DefaultService.GenerateEntity(entityMetadata, services);
            }
    
            bool ICodeWriterFilterService.GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
            {
                return this.DefaultService.GenerateOption(optionMetadata, services);
            }
    
            bool ICodeWriterFilterService.GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
            {
                return this.DefaultService.GenerateOptionSet(optionSetMetadata, services);
            }
    
            bool ICodeWriterFilterService.GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata,
            IServiceProvider services)
            {
                return this.DefaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
            }
    
            bool ICodeWriterFilterService.GenerateServiceContext(IServiceProvider services)
            {
                return this.DefaultService.GenerateServiceContext(services);
            }
        }
        //
    }
    
  3. Add codewriterfilter parameter to CrmSvcUtil.exe.config file. <add key="codewriterfilter" value="CRMExtensions.BasicFilteringService, CRMExtensions"/>
  4. Press F5 to debug the solution. It will generate a file  with the name specified in “o” or output parameter.
  5. Check the size of the file. It will be very very small as compare to the file generated without codewriterfilter. The size of the file created for default CRM online  organization with codewriterfilter is just 44 kb as compared to 4.7 mb without codewriterfilter.
Note if we replace the following line in BasicFilteringService.cs
if (!entityMetadata.IsCustomEntity.GetValueOrDefault()) { return false; }
with
if (entityMetadata.LogicalName!="account") { return false; }
The utility will generate just account entity. Happy programming.

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.

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" ?>
    <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>
    
  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".

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 "Working directory" of the project to generate the CrmProxy file in a desired folder.