How to: Add and Remove Host Items in an Add-in Project

You add host items to add-in projects when developers create items in the host application to contain objects. For example, a developer might add a document item to contain pages, or a form item to contain controls. When you add a host item to the add-in project, the developer can use it to customize that item. For more information, see Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project.

To add and remove host items, get the host adapter of the Visual Studio Tools for Applications IDE. You can then use the host adapter to access a collection of project host items. For more information, see Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project.

The code used in these procedures is taken from the VstaDesignTimeIntegration.cs file of the ShapeAppDynamicProgrammingModelCSharp sample. For more information, see How to: Build and Run the ShapeAppDynamicProgrammingModelCSharp Sample.

To add a host item to the project

  1. Get the host adapter by using the Extender property of the Project instance.

    IVstaHostAdapter hostAdapter = 
        (IVstaHostAdapter)project.get_Extender("VSTAHostAdapter2007");
    
  2. Call the AddProjectHostItem method of the ProjectHostItemCollection.

    The following method adds a host item that represents a drawing to a project in the ShapeApp sample application. This method uses the ShapeApp.Drawing object that is passed into the method to name the host item class.

    private void AddDrawingProjectHostItem(ShapeApp.Drawing drawing)
    {
        this.hostAdapter.ProjectHostItems.AddProjectHostItem(
            drawing.Name, 
            "Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp.DrawingEntryPoint",
            "Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint",
            drawing.Name,
            this.hostItemTemplatesPath + @"\CSharp\drawing");
    }
    

To remove a host item from a project

  1. Get the host adapter by using the Extender property of the Project instance.

    IVstaHostAdapter hostAdapter = 
        (IVstaHostAdapter)project.get_Extender("VSTAHostAdapter2007");
    
  2. Call the RemoveProjectHostItem method of the ProjectHostItemCollection.

    The following method removes a host item that represents a drawing from a project in the ShapeApp sample application. This method uses the Cookie property of the ShapeApp.Drawing object that is passed into the method to identify the drawing to remove.

    private void RemoveDrawingProjectHostItem(ShapeApp.Drawing drawing)
    {
        IVstaProjectHostItem item = 
            this.hostAdapter.ProjectHostItems[drawing.Cookie];     
        this.hostAdapter.ProjectHostItems.RemoveProjectHostItem(item);
    }
    

See Also

Tasks

How to: Add and Remove Host Objects in an Add-in Project

How to: Change the Display Name of a Host Item

Walkthrough: Adding Host Items and Host Objects to an Add-in Project

Concepts

ShapeApp Samples (Visual Studio Tools for Applications)

Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project

Creating In-Process Hosts

Defining Entry Points and Other Proxy Changes