Thursday 15 October 2015

Extending TDS glass entity generation

One of the really cool features of Hedeghog's TDS (Team Developer for Sitecore - https://www.hhogdev.com/products/team-development-for-sitecore/overview.aspx) is that it can create Sitecore Glass entities automatically, when you synchronize specific portions of the Sitecore tree.  

One of the issues I came across was that when it creates properties for Sitecore link and list fields like DropLink and TreeListEx; it uses GUIDs rather than the entity in question.  So how do you get the code generation templates to link to the entity rather than to a GUID?

There is a really simple fix that involves 4 steps.


Let's assume that you have an entity called Product Filter which has a TreeListEx field, which references a list of Brand items.
  1. In the Sitecore core database navigate to the Field Types folder under system and create a copy of the TreeListEx field naming it something like Brand TreeListEx

    N.B. I also create a new folder to hold all my Entity Types so that they are easy to find when editing a template:


  2. In the template that uses the field; change the type to be Brand TreeListEx (or whatever you called it).

  3. In Visual Studio navigate to the TDS project that contains the code generation templates. 

    N.B. This is probably your Sitecore templates project. 

    Open up the code generation template for the glass items - in my code this was called glassv3item.tt.  Scroll down the script until you find the following method:

    public static string GetGlassFieldType(SitecoreField field)
    
    Locate the switch statement add the following on a new line:
    case "brand treelistex":
         return "IEnumerable<Brand>";
    N.B. The text in the case statement must be lowercase and must exactly match the name of the newly created field in the core database.

    N.B Always use IEnumerable for collections.

    N.B. For a single item reference field like a DropLink; change the return part to be something like:

    return "Brand";
  4. Now synchronize the Sitecore items project so that the Sitecore Glass code generation is redone, and you should then be good to go.

No comments:

Post a Comment