Creating an Expression Web Add-in (3)

Before we go any further, let's briefly look at the Registry entries we just added in detail so that you'll understand what they do.

CommandLineSafe (DWORD)

The CommandLineSafe value indicates whether or not your add-in is safe to load when Expression Web is started from a command line. As a general rule, you would only want this to be 1 if your add-in doesn't implement any user interface elements and you expect it to load when Expression Web is run from the command line. I always set this to 0.

Description (String)

The Description value is required, but it really doesn't matter what you specify here. Some host applications (such as Visual Studio) will display the text you specify for the description in the Add-ins dialog. Expression Web does not use this value, but you have to specify it anyway.

FriendlyName (String)

The FriendlyName will appear in the Add-ins dialog in Expression Web when you select Tools, Add-ins.

LoadBehavior (DWORD)

The LoadBehavior values specifies how your add-in should load. Valid values are as follows.

0-  The add-in is disconnected (not loaded.)

1-  The add-in shows as connected (loaded.)

2-  The add-in will be loaded when the host application starts.

8-   Load on Demand. The add-in will be loaded programmatically or explicitly by the user.

16- The add-in is loaded the first time the host application starts and unloaded thereafter.

In our case, we want the add-in to be connected and loaded, so we chose a LoadBehavior of 3.

TipMuch of the documentation available on add-ins says that a value of 2 will set the application as connected and loaded on startup. In fact, setting the value to 2 will load the add-in on startup, but it won't connect it. As a result, your add-in won't work. You should set the LoadBehavior to 3 if you want your add-in to work on startup.

Accessing The Expression Web Application

In order to handle events for the Expression Web application, we will need to add a variable that will contain a reference to the application. We'll also need to ensure that we hook up the events that we want to handle.

Open Connect.vb or Connect.cs (depending on your language) and add the line of code below directly under the class declaration.

VB

Private WithEvents ewApp As Microsoft.Expression.Interop.WebDesigner.Application

C#

private Microsoft.Expression.Interop.WebDesigner.Application ewApp;

 

After adding this line of code, the top of your class file will look like the following code.

VB

Public Class Connect
  Implements Extensibility.IDTExtensibility2

  Private WithEvents ewApp As Microsoft.Expression.Interop.WebDesigner.Application

C#

public class Connect : Extensibility.IDTExtensibility2
{
  private Microsoft.Expression.Interop.WebDesigner.Application ewApp;

 

This line creates a new variable at the class level that will contain a reference to the Expression Web Application object. In VB, you can use the WithEvents keyword to indicate that you want to write code that runs when certain events (such as a Web site opening, closing, etc.) happen within the application. In C#, there is no corresponding keyword, so you will need to explicitly wire up the events that you want to handle. We'll take care of that a bit later on.

At this point, the ewApp variable is null. To populate it with a reference to the Application object, locate the OnConnection method and add the following code to it.

VB

ewApp = CType(Application, Microsoft.Expression.Interop.WebDesigner.Application)

C#

ewApp = (Microsoft.Expression.Interop.WebDesigner.Application)Application;

 

Notice that the OnConnection method takes two parameters, and the first is an Object called Application. The Application parameter contains a reference to the host application (Expression Web in this case), but we want to make sure that we use early binding on it. Therefore, we want to cast the Application object from OnConnection to the type Microsoft.Expression.Interop.WebDesigner.Application. The line you just added does that, and then it assigns that object to the ewApp variable. ewApp now contains a reference to the Expression Web Application object.

Testing The Add-in

Let's go ahead and test the add-in to make sure that it's working up to this point. To test the add-in, we'll display a message box when the add-in loads. In order to display a message box, you will need to add a reference to System.Windows.Forms to your add-in project. Using the instructions I gave you previously, add that reference. You'll find it on the .NET tab in the Add Reference dialog.

TipYou'll need to add a reference to System.Windows.Forms if you want to design dialogs for your add-in as well.

After you've added the reference to System.Windows.Forms, add the following line immediately under the previously added line in the OnConnection method.

VB

System.Windows.Forms.MessageBox.Show("Expression Web " & ewApp.Version)

C#

System.Windows.Forms.MessageBox.Show("Expression Web " + ewApp.Version);

 

NoteNote that the difference between the C# and VB code is very minimal. This is one of the advantages of the .NET Framework.

TipIf you want to use a shortcut, you can add an Imports statement for VB or a using statement for C# for the System.Windows.Forms namespace. Later in this article, we'll add that statement to make coding message boxes simpler.

Your OnConnection method should now look like the following code.

VB

ewApp = CType(Application, Microsoft.Expression.Interop.WebDesigner.Application)
System.Windows.Forms.MessageBox.Show("Expression Web " & ewApp.Version)

 

C#

ewApp = (Microsoft.Expression.Interop.WebDesigner.Application)Application;
System.Windows.Forms.MessageBox.Show("Expression Web " + ewApp.Version);

 

Save all of your work and make sure that you close Expression Web. After you've done that, select Build, Build Solution in Visual Studio. If you've done everything right, you'll see Build Succeeded in the Visual Studio status bar. After you see that, launch Expression Web. When it loads, you should see your message box display as shown in Figure 6.

NoteVisual Studio should show you where the problem is if you don't get a successful build. If you can't figure out what's wrong, download the sample project for your desired language and compare what you did to what I did.

The sample VB project is available here and the C# project is available here.

Figure 6 - Our Working Add-in

Figure 6

Our add-in is loading and working in Expression Web.

If your add-in doesn't work, there are a couple of things to check.

1. Make sure that your Registry entries are still in place.

2. Close Expression Web and then use the Processes tab in Task Manager to ensure there isn't a hidden instance of exprwd.exe running. If there is, end it and try again.

 

TipTo check Task Manager, right-click an empty area of the Task Bar at the bottom of your screen (by default) and select Task Manager from the menu. Click the Processes tab and look for exprwd.exe in the list. If you see exprwd.exe while Expression Web isn't running, you should end process on it and try your add-in again.

Once you test successfully, remove only the code that displays the message box. We're almost ready to add some real code and finish up the add-in, but first, let's configure the project so that you can debug your code in case something goes wrong.

Next Page >