Creating an Expression Web Add-in

In this article

What Is an add-in?

Creating an Add-in in Visual Studio 2005

Conclusion

Lagniappe: Installing An Add-in

Downloads For This Article

C# Add-in Visual Studio 2005 Project

VB Add-in Visual Studio 2005 Project

What Is an Add-in?

Years ago, Microsoft introduced a new programming model that was supposed to revolutionize programming. It was (and still is) called Component Object Model, or COM. I won't go into all of the details of COM (it would take a book all its own), but suffice it to say that Microsoft built many applications with COM architecture in mind.

Office applications (and I'll include Expression Web in that category) are no exception. Office applications expose a lot of functionality that you can tap into programmatically by creating a DLL file that hooks into them. That DLL file is called a COM add-in.

If you've ever created a macro using VBA in an Office application, you are already familiar with the kind of code you write to interact with an Office application. If you're not familiar with writing code against the Expression Web object model, don't worry. We'll examine some of it as we progress through this article.

TipIf you'd like some details on writing code against the Expression Web object model, you can get that information from my book on Expression Web. I have a full chapter dedicated to that topic and I walk you through the process.

You can also dig into the object model using the Object Browser in Visual Studio.

Creating An Add-in in Visual Studio 2005

You can create a COM add-in with Visual Studio 6, but with the benefit of the .NET Framework and the ability to take advantage of the class libraries it offers, why would you? It's easy to create a COM add-in using your favorite managed language (I prefer C#) and Visual Studio 2005.

TipAdd-ins created with Visual Studio 2005 will require the user to have the .NET Framework 2.0 installed. Since this is a requirement for Expression Web, you don't have to worry whether or not your users will have it installed. If you are creating an add-in for another Office application, you'll want to make sure that you document your requirements and check for them in your installation package.

The easiest way to learn how to create an add-in is to actually create one, so let's create a simple add-in that will handle some of the events exposed by Expression Web. We'll use a simple implementation that displays message boxes when certain events occur.

NoteIf you'd like, you can download the VB version of the completed add-in here or the C# version here.

Creating the Visual Studio Project

Since an add-in is a DLL that is registered with Expression Web, you'll want to create a new Class Library project in Visual Studio 2005.

1. Open Visual Studio and select File, New, Project from the menu.

2. In the New Project dialog, select Windows in the list of project types, Class Library as the template, and enter SampleAddin as the name for your add-in as shown in Figure 1.

Figure 1 - Creating an Add-in Project

Figure 1

Create a new project by choosing the Class Library template in the Windows project types.

3. Click OK to create your project.

Changing the Default Class File

By default, Visual Studio creates a new class for you called Class1. You're going to want to change this to a more appropriate name. If you use Microsoft's naming convention, the main class for your add-in should be Connect. Let's make that change now.

In the Code window in Visual Studio, you'll see the following code in Class1.cs or Class1.vb depending on your language.

C#

public class Class1

VB

Public Class Class1

 

You will want to change Class1 to Connect so that your code looks like the following.

C#

public class Connect

VB

Public Class Connect

 

After you do that, save the file and then rename Class1.cs or Class1.vb to Connect.cs or Connect.vb, once again depending on your language.

TipRemember that C# is case-sensitive!

 

Adding References

Before you can start writing code for the add-in, you need to add some references to the project.

All COM add-ins implement the IDTExtensibility2 COM interface. Don't worry if that's all Greek to you because understanding it is not necessary. It's a simple process, but to do it, you need to add a reference to the Extensibility library. Here's how you do that.

1. In Visual Studio, select Project, Add Reference.

2. On the .NET tab, select extensibility as shown in Figure 2 and click OK.

Figure 2 - Adding a Reference to Extensibility

Figure 2

You'll need to add a reference to the Extensibility library in order to create your COM add-in.

You'll also need to add a reference to the core Office library as well as to the Expression Web libraries.

1. Select Project, Add Reference.

2. Select the COM tab.

3. Select Microsoft Office 12.0 Object Library as shown in Figure 3 and click OK.

Figure 3 - Adding a reference to the Office library.

Figure 3

Add a reference to the Office library.

Last but not least, you'll want to add references to Expression Web. In this particular case, we need a reference to the Microsoft Expression Web 12.0 Web Object Reference Library and the Microsoft Expression Web 12.0 Page Object Reference Library. You'll find these libraries on the COM tab in the Add Reference dialog.

NoteI intentionally had you add these references explicitly, but in fact, when you add the reference to the Microsoft Expression 12.0 Web Object Reference Library, Visual Studio will automatically add a reference to the Office 12.0 library and other libraries that it needs.

TipYou can right-click your project in Solution Explorer and choose Add Reference to more easily add a reference.

In C#, you'll see references listed as a node in Solution Explorer by default. For some reason, the References node is hidden in VB by default.

Next Page >