Creating an Expression Web Add-in (2)

Implementing the Interface

Now that all of the required references have been added, it's time to implement the IDTExtensibility2 interface. This is accomplished a bit differently depending on your language.

In VB, open Connect.vb and add the following code directly underneath the class declaration for the Connect class.

Implements Extensibility.IDTExtensibility2

 

After you do this, your Connect.vb file should look like this:

VB

Public Class Connect

    Implements Extensibility.IDTExtensibility2

 

Once you add the code and press Enter, the methods that you must implement for this interface will be added for you automatically.

In C#, you'll take a little bit different approach. Open the Connect.cs file and change the class declaration to the following.

C#

public class Connect : Extensibility.IDTExtensibility2

 

Once you add this, you'll notice a small, blue line under the "E" in Extensibility. Hover over that to get the options button and select Explicitly implement interface 'Extensibility.IDTExtensibility2' as shown in Figure 4 to add the necessary code for the interface.

Figure 4 - Implementing the IDTExtensibility2 Interface

Figure 4

C# will add the necessary code for the interface once you select to explicitly implement the interface.

NoteAfter you implement the interface in C#, Visual Studio will automatically create a collapsible region called IDTExtensibility2 Members.

Now you've got a lot of new code in your class file. When you implement an interface, you are required to provide implementation for all of the methods in that interface. That's why Visual Studio has added all of this code for you.

In VB, you get a bunch of empty methods. In C#, Visual Studio adds one line of code to each method that looks like this:

throw new Exception("The method or operation is not implemented.");

 

Exceptions are expensive and I don't want to throw one for each method that I haven't implemented. Therefore, go ahead and remove all of the lines that look like the above code and save your code file. Your add-in will work fine even if you don't provide any code in the implementation for the methods.

CautionMake sure that you only delete the line of code that looks like the code above! If you delete anything else, your add-in will not work.

Configuring the Project for COM

In order for your add-in to work with Expression Web, the DLL that you're creating needs to work as a COM DLL. However, Visual Studio 2005 doesn't create COM code. It creates managed code that runs under the .NET Framework, and it's quite different than COM. Fortunately, Microsoft lets you create a managed DLL in such a way that it looks like a COM DLL, and doing it is very easy.

1. Select Project, SampleAddin Properties in Visual Studio 2005.

2. In the Properties window that is displayed, select the Compile tab if you're using VB and the Build tab if you're using C#.

3. In the Configuration dropdown, select All Configurations.

4. At the bottom of the dialog, check the Register for COM interop checkbox.

CautionMake sure that you select All Configurations. Otherwise, by default, the add-in will only work when you build it in Debug mode.

Now you will need to make the DLL visible to COM. To do that:

1. Select the Application tab in the properties window.

2. Click the Assembly Information button.

3. Check the Make assembly COM-visible checkbox.

4. Click OK.

5. Close the properties window and save your project.

Your add-in is now configured properly to work as a COM DLL.

Adding the Registry Keys

In order for Expression Web to recognize your add-in, you will need to add a Registry entry for it. Actually, COM will add Registry information for your DLL as well, but that happens automatically. The only Registry entries you will need to explicitly add are those that control how it gets loaded into Expression Web.

The Registry entries you will need to make are located at the following location:

HKEY_CURRENT_USER\Software\Microsoft\Expression\Web Designer\Addins

The settings for your add-in are added to a new key under the Addins key. The name of that key is in the format of AddinName.ClassName. Therefore, for our add-in, you will need to add a new key called SampleAddin.Connect under the Addins key. After you've added that new key, add the following values.

1. A new DWORD value called CommandLineSafe with a value of 0.

2. A new String value called Description with a value of Sample Add-in.

3. A new String value called FriendlyName with a value of Sample Add-in for Expression Web.

4. A new DWORD value called LoadBehavior with a value of 3.

After you've added all of these, your Registry entries should look like those shown in Figure 5.

Figure 5 - Registry settings for the sample add-in.

Figure 5

Registry entries for your COM add-in.

TipAfter you add your Registry settings, you might want to export them to a file so that you can easily add them back again. To do that, right-click on the SampleAddin.Connect Registry key and select Export. Save it to a safe location. You can then add the entries back to the Registry by double-clicking on the file.

Next Page >