There are many times within not only Sitecore development, but typical development where it is nice to have pre-stubbed classes. Within these classes you would e.g.
- Implement all virtual or abstract methods
- Have predefined using statements
- Have comments
- Have helper classes imported and ready to use
Luckily, within Visual Studio we have a simple way of creating templates and then export them for use within you projects. How many times have you asked yourself, “What class do I have to implement for a custom validation rule?” or “Great, I have implemented my abstract class, but what do I do now?”. This is where the power of templates can be used. I am simply talking about the typical templates within Visual Studio, this is nothing new or cutting edge. So what I will do to spice this blog up is to also show you how to generate these classes with the T4 Templates system in Visual Studio.
Create Code Templates the Old way.
Open Visual Studio 2008 and create a new project based on the C# Class Library Application Project.
Create a class and call it SitecoreToken.cs.
What we are going to do is create a class template the will allows Sitecore developers to simply create new tokens in the Sitecore system e.g. $name will return the name of the content item. We will create one that returns the content user.
Stub the class out as follows.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using NVelocity;
6 using NVelocity.App;
7 using Sitecore.Data.Items;
8 using Sitecore.Pipelines.ExpandBranchItemName;
9 using Sitecore.Workflows.Simple;
10
11 namespace SitecoreClasses
12 {
13 public class SitecoreToken : ExpandBranchItemNameProcessor
14 {
15 public override void Process(ExpandBranchItemNameArgs args)
16 {
17
18 }
19 }
20 }
21
22 #region Sample
23
24 //public class CurrentUser : ExpandBranchItemNameProcessor
25 //{
26 //public override void Process(ExpandBranchItemNameArgs args)
27 //{
28 // //if (args.BranchTemplateItem.Name.Equals(“$tokenname”))
29 // // args.Result = Sitecore.Context.User.Name;
30 //}
31 //}
32
33 /* You will then need to add the following line to your web.config in the following section
34
35 <expandBranchItemName>
36 <processor type=”Sitecore.Pipelines.ExpandBranchItemName.ReplaceVariables,Sitecore.Kernel” />
37 ...
38 <processor type=”YourNameSpace.YourClassName, YourAssembly”/>
39 *…
40 </expandBranchItemName>
41
42 * <expandInitialFieldValue help=”Processors should derive from Sitecore.Pipelines.ExpandInitialFieldValue.ExpandInitialFieldValueProcessor”>
43 <processor type=”Sitecore.Pipelines.ExpandInitialFieldValue.SkipStandardValueItems,Sitecore.Kernel” />
44 <processor type=”Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables,Sitecore.Kernel” />
45 *…
46 *<processor type=”YourNameSpace.YourClassName, YourAssembly”/>
47 *…
48 </expandInitialFieldValue>
49 *
50 */
51
52 #endregion
Let’s have a look at what we are stubbing out in our class. Firstly, we are importing libraries that are required for creating a token in the system. Secondly, we are making sure that we extend the ExpandBranchItemNameProcessor class and override the Process method so our developers don’t have to discover this for themselves. Thirdly, I want to stub out a sample of how to create a token in the system. Finally, I want to give directions to the developer on what to do from here. In the example above, they need to add lines to the web.config to register their new token. (Of course, as a best practice you should create a separate tokens.conig file and put it in the App_Config/Include directory so that you don’t blow out your web.config file.
Some ideas for other things you could add into your class include :
- Commonly used Managers e.g. LinkManager, ItemManager etc.
- Commonly used variables e.g. Home item, Context Item, Context Database
- Static helper classes that you have built inhouse
- Best practices section
- Coding standards examples
For my example, I will keep it simple. In fact, once we have a bit more time I will stub out classes with all these details.
Happy with your template? Great. In Visual Studio, select File->Export Template. You will be taken through the following Wizard.
Step 1 : Select the project that you created in Visual Studio from the drop down list. Make sure “Item Template” is selected.
Step 2 : Select the class for which you would like to create a template for.
Step 3: Select the reference libraries that you would like to be referenced in your template (WARNING : You will receive a warning if there are libraries that are not included in the core .net framework. This simply means that when a user adds this template to their own project, they will have to add those references as well.)
Step 4: Next, you will want to specify an icon, a template name (This is what will appear in Visual Studio) and a description (This will also show in Visual Studio).
Click Finish.
A zip file will be exported onto your file system. All you need to do is copy it into your C:\Users\XXXXXX\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C# folder.
When you now add a new item to a project then you will now see all your templates in the dialog box like so ->
Select the Sitecore Token template and enter UserToken as the class name it will generate the following class for you ->
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using NVelocity;
6 using NVelocity.App;
7 using Sitecore.Data.Items;
8 using Sitecore.Pipelines.ExpandBranchItemName;
9 using Sitecore.Workflows.Simple;
10
11 namespace SitecoreClasses
12 {
13 public class UserToken: ExpandBranchItemNameProcessor
14 {
15 public override void Process(ExpandBranchItemNameArgs args)
16 {
17
18 }
19 }
20 }
21
22 #region Sample
23
24 //public class CurrentUser : ExpandBranchItemNameProcessor
25 //{
26 // public override void Process(ExpandBranchItemNameArgs args)
27 //{
28 // //if (args.BranchTemplateItem.Name.Equals(“$tokenname”))
29 // // args.Result = Sitecore.Context.User.Name;
30 // }
31 //}
32
33 /* You will then need to add the following line to your web.config in the following section
34
35 <expandBranchItemName>
36 <processor type=”Sitecore.Pipelines.ExpandBranchItemName.ReplaceVariables, Sitecore.Kernel” />
37 …
38 <processor type=”YourNameSpace.YourClassName, YourAssembly” />
39 *…
40 </expandBranchItemName>
41
42 * <expandInitialFieldValue help=”Processors should derive from Sitecore.Pipelines.ExpandInitialFieldValue.ExpandInitialFieldValueProcessor”>
43 <processor type=”Sitecore.Pipelines.ExpandInitialFieldValue.SkipStandardValueItems,Sitecore.Kernel” />
44 <processor type=”Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables,Sitecore.Kernel” />
45 *…
46 * <processor type=”YourNameSpace.YourClassName, YourAssembly”/>
47 *…
48 </expandInitialFieldValue>
49 *
50 */
51
52 #endregion
Creating Templates the new way (T4 Templates).
First of all you will need to download the following tools ->
T4 Toolbox -> http://www.codeplex.com/t4toolbox
T4 Editor -> http://www.olegsych.com/2009/04/t4-editor-by-tangible-engineering/
Once you have done this, Open Visual Studio 2008 and create a new class but give the class name of SitecoreTokenTemplate.tt. With the applications installed above, it will know to use t4 to create a template for you.
The huge benefit of using t4 is that you can run code to populate your class. Place the following code into the .tt file.
Use the same process for exporting the template as described above. Now when you create a class baseoff this template you will see the following (note that the DateTime and
Filename code has actually ran):
1
2 //<autogenerated>
3 // This file was generated using SitecoreToken.tt.
4 // Any changes made manually will be lost next time the file is regenerated.
5 // </autogenerated>
6 // The sitecore information is 12/16/2009 13:23:50. The end.
7
8
9 using System;
10
11 namespace ConsoleApplication4
12 {
13 public class Class1
14 {
15
16
17
18 }
19 }
You are probably asking yourself “This really isn’t Sitecore is it?”. The answer is, not really. However, check back here soon and you will be able to see some pretty crazy things we can do with t4 and Sitecore.
To whet your appetite in the meantime, I have attached a zip package that contains lots of Sitecore templates, including templates for creating :
- Workflow Actions
- Validation Rules
- Conditional Rule Renderings
- Analytic Filters
- Sitecore Tokens
- Sitecore Web Controls
- Http Pipeline Processors
You can download the package here. Simple unpack the main zip and move all the contained zips into the following directory
C:\Users\XXXXXX\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C# folder.














