A simple trip into the Visual Studio 2008 Item Template Area

I recently ran across a very well done video, except the developer was using VB and I’m a C# person, about how to create your own Visual Studio 2008 Item Templates. After watching the video I decided to play around with the StsDev2008 projects and see if I could create a series of item templates for each of the special projects that get created.


My first attempt was to create a SharePoint Feature Receiver. This is simple enough to program, but I’m often copying the same code snippet:

SPWebApplication webApp = null;


If(properties.Feature.Parent is SPSite)



Else if(Properties.Feature.Parent is SPWebApplication)




Foreach(SPWebConfigModification mod in CreateConfigModifications())


//Add or Remove based on whether this is the Activated or DeActivated function






Instead I wanted my FeatureReceiver to always have this code in place and then all I would have to do is implement the CreateConfigModifications function.


I began by creating my StsDev2008 Simple Feature Project and selected one of the deployment locations (not really important where it was going to since this is just an item template) and I also made sure I had the Include Feature Receiver checked. I then added the custom code (shown above) into the FeatureActivated and FeatureDeactivated functions. Then I created a shell function for the CreateConfigModifications with a List<SPWebConfigModfication> which is created and returned.


Next I changed the namespace that was provided to $rootnamespace$ which is a Visual Studio Item Template Replacement Macro. The $rootnamespace$ will be replaced when the user creates the new item with the project’s namespace.


Next I changed the class name to $safeitemname$ which will be replaced with the name the user specifies when they create a new object.


Finally I used the Export Wizard as described in the video to export the project. After the export wizard was complete I tried the Add Item option on the project, and there was my new SharePointFeatureReceiver item.


A more complex trip into the Visual Studio 2008 Item Template Area

After creating my first Visual Studio Template Item I decided that creating another would be interesting as well. Since I had already done the feature receiver I didn’t feel that creating the web part item template would be as interesting, so I decided to create the Layout Page template.


A layout page is a standard aspx page that is stored in SharePoint’s _layouts folder. This page usually consists of an aspx and a cs page (in the case of C#) which the aspx page inherits from. This means my template must include two files which is why this is a more complex activity.


I begin by creating a new StsDev2008 project with the layout pages, by default StsDev2008 places the aspx in the LAYOUTS folder in your project and places the class file in the root of the project. The means that the two files are not recognized as being linked in Visual Studio 2008. Therefore when you run the export wizard what you find is that only the aspx or the class file are exported. I find that exporting the aspx is the best method. Before exporting we do need to make some changes, first we need to update the aspx <%@ Page …%> tag with some replacement macros.


In the Inherits value in the <%@ Page … %> tag replace the current value with $rootnamespace$.$fileinputname$. This will allow the proper substitutions to occur when the item is added to the project. Now I tried, but failed, to change the <%@ Assembly Name=”…” %> so that it would reference the project name. However, it appears that this does not work (I am still looking into this to see if I can resolve the problem). Because of this limitation I just replaced the assembly name with [Your Assembly] so that a user knows they need to manually replace that value. Now you can export the aspx as an item template.


Finally, modify the class file in the same way as documented in my previous blog.


By this point you should have created and exported the Item Template so we need to discuss how Visual Studio Templates work. First, the template object is nothing more than a zip file. Inside the zip file is an icon file (.ico) and a template file which is similar to a SharePoint manifest file. Also inside are one or more class, aspx, ascx, etc files which contain the Visual Studio Template Macros. For this blog we will add an additional template item file and update our template manifest file.


First you can drag and drop your class file, the one that actually implements the inherited class referenced by your aspx file, into the Item Template zip file. Next you can extract the .vstemplate from the zip file. By double clicking on the vstemplate file Visual Studio will open the file and you will immediately notice it is a simple XML file.


Example of my .vstemplate:

<VSTemplate Version=”2.0.0” Type=”Item” xmlns=”…”>



<Name>SharePoint Layout Page</Name>

<Description><SharePoint Layout Page</Description>








<ProjectItem …>




The ProjectItem tags are what we are interested in. More than likely you have one that references your aspx file. Replace the ProjectItem with the following xml


<ProjectItem TargetFileName=”$fileinputname$.aspx” ReplacementParameters=”true”>AppPage1.aspx</ProjectItem>

<ProjectItem TargetFileName=$fileinputname$.aspx.cs” ReplacementParameters=”true”>AppPage1.aspx.cs</ProjectItem>


What you have just done is to allow the user to change what the filename is, but you will restrict them to using an aspx and aspx.cs extension for the layout page and class file respectively.


Once you have saved the template file you can copy it back into the zip file.


Now if you are like me and edited the templates in the export folder you are now wondering why your changes didn’t get committed. The reason is that export folder has a copy that is not used of the template. Visual Studio actually uses the templates that appear in the %USERDOCS%/Visual Studio 2008/Templates/ItemTemplates. You may edit this location directly, but if you have already modified your Exported Template item then just copy from the My Exported Templates into the Templates/ItemTemplates folder and you are off and running.