Hardcore CRM: Playing with meta data

One of the advantages of being a software engineer is that you can automate your job. Let some handy tooling do a lot of the dirty work, allowing you to focus on stuff that matters.

Back in the days I used to use metadata to automate my job, which is writing code (below an example of one of my old generators).

Codegenerator

As soon as I could lay my hand on a solid relation datamodel, I used to write code to generate code. This resulted in solid and easy to maintain software projects.
A few weeks ago I was training some developers in extending Dynamics CRM. One of the tools being discussed in the training is the SDK tool CrmSvcUtil.exe. A command line tool that you can use to generate C# code, that you can use in your projects. A nice tool, however the tool is generating C# code for every entity available in CRM. Creating a lot of overhead.

As a product developer, I’m not interested in having code for any entity in CRM, I’m interested in having just the code for all entities of the solution I’m working on. I abandoned the CrmSvcUtil tool, and kept thinking of the code generators I wrote in the past.

Instead of reinventing the wheel, I did some research on the web and stumbled on a blog post, in which Simone Tagliaro described how to get all entities for a solution.
Though it was written for CRM 2011, it works like a charm on CRM 2015. This can serve as a perfect basis for the new tool I’m planning to build; An easy to use and flexible code generator powered by Dynamics CRM.

I created a test solution on CRM, containing three entities, fiddled a bit with the code and gave it a shot. I was pleasantly surprised.
I was able to retrieve the metadata for the entities within the solution I defined’.

In the figure below an example of the account entity with the fields specified.

Generatoroutput

The code used for this, turned out to be pretty straightforward.

Generatorsample

In step 1 all solution components (with componenttype=1 (entity)) within the solution (identified by its uniquename) are being retrieved. In step 2 all entities with all metadata within CRM are being retrieved. Finally in step 3 a Linq query is used to filter only those entities that belong to the solution.

Once the data is retrieved, I can iterate through the entities and all attributes. Getting the data I want.

Time to hammer out a new code generator…

7 thoughts on “Hardcore CRM: Playing with meta data

  1. […] me a celebration is no celebration without any gifts. Therefor inspired by my last article, I decided to create a small gift for […]

  2. malek mouaddeb says:

    I tried this code and it only retrieve entities, no process, no web resources no any other component.

    I changed the componenttype other than the value 1 (for entities)

    But it geves nothing.

    Can you help me, i real.ly need all components
    thanks

    • Hi Malek,

      What do you want to achieve? Perhaps I can help

      Bas

      • malek mouaddeb says:

        In fact, I’d like to move all the customizations from one instance OnLine to another only in code c#.
        I checked out in the internet and I found that the best way is to create a solution and add all the customizations from the default solution in it then transfer it to the other instance.

        So I tried your solution, and it effectively retrieved all entities, but only entities, no process, no web resources …

        I want to move all default solution components.

        Any help please. I really need it.
        And thanks.

        • malek mouaddeb says:

          I got no reply.

          At least, can you please explain this line, I don’t get what means those x and y :

          //Join entities Id and solution Components Id
          returnAllEntitiesresponse.EntityMetadata.Join (ComponentsResult.Entities.Select
          (x => x.Attributes[“objectid”]),
          x => x.MetadataId,
          y => y,
          (x, y) => x);

  3. […] information of the CRM solution I’ve played with this as well. In an earlier blog post I posted a piece of source code to get all meta data information of an […]

Leave a Reply

Your email address will not be published. Required fields are marked *