October 27, 2006

dotNET Project Template and Addin Creation

Project and Item Templates
Create Reusable Project and Item Templates for the development team.

Project Template Creation
Creation of a project or item template is pretty straight forward.

Use the File->Export Template to create the template, the wizard will ask you which Project you wish to base your Template on, this will result in the contents of that Project being added to the Template.
Note that depending on the type of Project you select will determine where the Template appears in the File->New Project dialog.
i.e. If you base your Template on a Website Project in VB then the Template will appear in the File-New Website and under the VB section in the File->New Project dialog.

Create a project like the one found at

The Template is just a zip file with an extra xml file within, the xml file describes what to add to the project when a user uses File->New Project.

If you wish to generate Project Templates at build time from another project it can be done through MSBuild. I've written a description of this in the MSBuild Post, ZipProject is the MSBuild task.

Editing the Contents of the Template File
The File->Export Template will create the template for you. When the user uses this template they are not prompted for anthing besides the Project name, what if you wish for the user to supply some additional information when creating a new project using your template? It's not very easy...
Basically you have to create a seperate class which is a type of Form, it's an implementation of IWizard. The assembly generate needs to be registered in the GAC and then a reference to this Assembly added to your Template file, see this msdn link:
Note that one of the steps is the Signing the Assembly, use the VS feature in the Project Properties for this. And then run C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /i

Now we have installed the assembly. After the assembly is installed you should use the File->Export Template. After that youwill need to edit the XML file, so unzip the file at and edit the xml file Add the assembly name details which were generated with the Project->Properties->Signing feature, to see what the value is used gacutil again
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /l WizIDE

And add the result to the xml file after , that is outside of TemplateContent, like the following:
WizIDE, Version=, Culture=neutral,

Now Rezip and open a new project of this type. If you change the assemble functionality then maybe a reinstall is required C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /uf WizIDE

Web Application Template Creation
The template is generated in the same manner as the project template, select your website project and use File->Export Template.
As with the project template, templates can be created at build time from your project using MSBuild, see my MSBuild post, ZipProject is the MSBuild task, you'll see that the Web application template is a bit more difficult to configure from MSBuild, there are alot of workarounds to include subfolders in the resultant zip file.

String Substitution in Templates
Project Template also provide the ability to substitute strings for strings in the files included in the Template.
Your .template file which is created for you (and included in the .zip file) contains a list of all files in the Template. It also contains some properties of each file including a boolean which tells the Template installer to substitute certain strings when the Template is used.
(open tag)
ProjectItem ReplaceParameters="true" TargetFileName="app.aspx">app.aspx
$safeprojectname$ gets replaced with the name you give to your project.
The original name of your project was "Server".
You generate a Project Template from Server.
The Template Generator sets all the file entries in the template file to have substitution on.
The Template Generator sets replaces all entries of the word "Server" in all of those file with the placeholder
You create a new Project using the Template and name it "MyNewProject".
When you create a Project with the Template the placeholder
$safeprojectname$ is replaced with the name of your project.

in the asp.aspx.cs file
using MyNameSpace.Server; is replaced with MyNameSpace.MyNewProject;

Original app.aspx.cs in Project named "Server".
using System.Collections.Generic;
using MyNameSpace.Runtime;
using MyNameSpace.Server;

The app.aspx.cs generated and add to the Template"Server".
using System.Collections.Generic;
using MyNameSpace.Runtime;
using MyNameSpace.

The new app.aspx.cs in new Project named "MyNewProject".
using System.Collections.Generic;
using MyNameSpace.Runtime;
using MyNameSpace.


Other properties which can be substituted are:
msdn Template Parameters.
msdn (
Modifying or Creating a Visual Studio Project Template).

Addin Creation
Addin Creation To create custom controls, menu items etc use the Addin. There is an Addin Project in VS, create a new Addin Project from scratch and compile. To register the new addin choose Tools->Addin Manager, select the new Addin from the list and restart VS, the Addin Tools items now appear in the menu. The Addin’s .addin file has been copied to
C:\Documents and Settings\\My Documents\Visual Studio 2005\Addins

Addin examples can be downloaded from

Item Template Creation
In order to add a custom item to the New Item command we have to create another template and Item Template. I have developed this in particular for the Entity creation.
It is virtually identical in makeup to the Project Template but with a different form for data entry. The installation process is the same as the Project Template earlier except this time you create an Item Template instead of a Project Template when using File->Export Template.

When you create a Template the template .zip file gets copied into 3 locations where VS will detect them
The original location:
C:\Documents and Settings\me\My Documents\Visual Studio 2005\My Exported Templates
the Visual Studio Project Template location (This is an optional step in the Export Template Wizard):
C:\Documents and Settings\me\My Documents\Visual Studio 2005\Templates\ProjectTemplates
and also a cache location
C:\Documents and Settings\me\Application Data\Microsoft\VisualStudio\8.0\ProjectTemplatesCache
The Cache location may cause confusion, if you no longer want to use the Template and decide to delete it you must also delete from the Cache location.

Problem here when referencing the DLLs, because we have made the Templates “Strongly Named” we cannot reference the “Weakly Named” assemblies. Solution?? Currently there are 3 projects. WizIDE which creates a Project Template, a dll, WizIDE.dll which is added to the Project Template as the Zip file WizIDETemplate.zip. IDEAddin, creates a DLL, IDEAddin.dll, this is is installed through the VS Addin Manager, it is added as an .addin file to the
required, presently it adds a new item to the Project menu which does nothing right now. EventItemTemplate, creates a DLL, EventItemTemplate.dll, this is installed in the same manner as the Project Template at C:\Documents and Settings\\My Documents\Visual Studio 2005\Addins.
Creates the extra menu items which maybe
C:\Documents and Settings\\My Documents\Visual Studio 2005\Templates\ItemTemplates\EventItemTemplate.zip
This provides the functionality to create new Entity through a wizard, New Item dialog now contains another item type, EntityItemTemplate which launches a wizard and prompts for Entity parameters.

Installing the Project and Item Templates
If you create your Project and Item Templates using MSBuild from your source projects using ZipProject and ZipItem then the templates will be installed for you by MSBuild into the correct location (note a copy of these files can be found in your source projects obj/Debug or whatever directory). But of course you'll want to install these templates on a clients machine using an installer of something equivelant., the solution is to copy the files into the Visual Studio installation from your installer using a Custom Action (the custom action is a project you create, it inherits installer and can be called from the installer). You'll also need to reset the Visual Studio Cache, this can be done from the Custom Task code too

System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo(Path.Combine(vsDevenv, "devenv.exe"));
ps.Arguments = @"/setup";
System.Diagnostics.Process.Start(ps).WaitForExit(10000);//give the process a max of 10sec to finish

October 24, 2006

dotNET Custom Controls and Resource Scripts

Custom Controls are Web Controls, (TextFields, Labels etc) which are customized by you.

The Custom Controls can then added to your website by adding the assembly containing the Custom Control or used through Visual Studio ASPX editor by created adding the Custom Control to the ToolBox (this is done by Right-Clicking the ToolBox and selecting Choose Items... then select the locate of the Assembly containing the Custom Control.

An example of a very simple Custom Control implementation can be found here
Simple Custom Control (msdn)

Additionally you can embed some of you JScript code in a .js file instead of in the aspx page itself. This js file should be added to you Custom Control project as an Embedded Resource (This means the JScript code is embedded in the resultant assembly. When this assembly is referenced by a website project it will be able to access the JScript functionality (??).
To add the js file add it to the Custom Control Project as a Resource.
Add a new Resource file to the Project.
Add you js file to the project. Set the Property of the js file, "Build Action" to "Embedded Resource".
Add the js file to the Resource file using the Resource Designer, Choose Add Existing Item.. from the menu at the top of the Resource Designer.
Add a Line to the AssemblyInfo.cs file,
first include:

using System.Web.UI;

to the top of the file
[assembly: WebResource("WebControlLibrary1.Resources.JScript1.js", "text/javascript", PerformSubstitution = true)]

Now to enable accessing the JScript contents from and ASP.NET file you must Register it with your Custom Control, do this in the OnPreRender Overload method using

protected override void OnPreRender(EventArgs e)
Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "jscriptcontrol", Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary1.Resources.JScript1.js"));

Now rebuild. Your JScript file is now embedded in the assembly and can be accessed from ASP.NET using when you add the Custom Control to the aspx page using

(open bracket)
cc1:WebCustomControl1 ID="WebCustomControl1_1" runat="server"

(close bracket)

Adding JScript files and other resources to an assembly ans accessing from ASP.NET
Adding and Accessing Embedded Resources
Adding Client Scripts to ASP.NET

To test your embbedded resource is actually correct:
Drag and drop the Custom Control which references the JScript into a new aspx page.
Publish the website with the new aspx page.
Navigate to it's url in a browser, when it opens View-Source.
In the source locate the script src tag.

(start script tag)
src="/UseGalaxyWebControl/WebResource.axd?d=WS6VQ6ceILK4cpcSIBDTOA0gOitJm62vmYH2X7E8EUOB2p55z1WYfEnbnNTg7cQs-HZKaxcnh4sTcOoa0Og_HVTyCk7NIZnZpc-1MUQd7bw1&t=632974654934687088" type="text/javascript">
(end script tag)

Copy the link you find in here. Copy the link back into the browser.
Hit return and you should see the contents of the JScript file you embedded in the assembly.


This works because the ASP.NET 2.0 has an assembly reflection type service WebResource.xad. This uses the url passed into the service to extract it from the assembly.
For more see this description
Using the WebResource.axd Handler with Embedded ASP.NET Resources (eggheadcafe.com)

So now you can drag and drop the Custom Control onto your ASPX page in Visual Studio designer. What good is that?
Your aspx page now has access to all your JScript functionality. You can call your JScript functions in the aspx page as normal using the tag.

Resource Caching:
So you've embedded the resource and added it to your aspx.
ASP.NET 2.0 also provides some caching functionality by default.
If the application is in Release (the web.config debug="false" set) all the resources are cached, meaning they will not be reloaded (GET) each time the page is POSTED as long as the resources have not changed.
In debug mode the resources are not cached, so they are always reloaded on page POST.
You'll see in the url for the resource, it contains a t=... this t is the time. This is what's used to compare the cached version of the resource and the latest version on the server.

October 19, 2006

dotNET Custom Configuration Handlers

So you want to add some configurable content to your application/Web application.
The place for your data is in the configuration file, app.config or web.config.
You could put it in the AppSettings, a small problem with this is it uses Key/Value collections which means that the Keys must be unique, that's not the end of the world but you may want to access a complete section in the config file and sort them or something.
The alternative is to create your own section in the config file. To access this you'll need to create your own Handler Class.
I'll explain the settings in the config file followed by the Handler Class and then how to access it.

Custom Configuration Handlers on ASP.NET Forum