Walkthrough: Binding to Data from a Service in a VSTO add-in Project
You can bind data to host controls in VSTO Add-in projects. This walkthrough demonstrates how to add controls to a Microsoft Office Word document, bind the controls to data retrieved from the MSDN Content Service, and respond to events at run time.
Applies to: The information in this topic applies to application-level projects for Word 2010. For more information, see Features Available by Office Application and Project Type.
This walkthrough illustrates the following tasks:
Adding a RichTextContentControl control to a document at run time.
Binding the RichTextContentControl control to data from a Web service.
Responding to the Entering event of a RichTextContentControl control.
|
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE. |
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configuring a Computer to Develop Office Solutions.
Word 2013 or Word 2010.
The first step is to create a Word VSTO Add-in project.
To create a new project
Create a Word VSTO Add-in project with the name MTPS Content Service, using either Visual Basic or C#.
For more information, see How to: Create Office Projects in Visual Studio.
Visual Studio opens the
ThisAddIn.vborThisAddIn.csfile and adds the project to Solution Explorer.
For this walkthrough, use a Web service called the MTPS Content Service. This Web service returns information from a specified MSDN article in the form of an XML string or plain text. A later step shows how to display the returned information in a content control.
To add the MTPS Content Service to the project
On the Data menu, click Add New Data Source.
In the Data Source Configuration Wizard, click Service, and then click Next.
In the Address field, type the following URL:
http://services.msdn.microsoft.com/ContentServices/ContentService.asmx
Click Go.
In the Namespace field, type ContentService, and click OK.
In the Add Reference Wizard dialog box, click Finish.
In VSTO Add-in projects, you add and bind controls at run time. For this walkthrough, configure the content control to retrieve data from the Web service when a user clicks inside the control.
To add a content control and bind to data
In the
ThisAddInclass, declare the variables for the MTPS Content Service, the content control, and the data binding.Add the following method to the
ThisAddInclass. This method creates a content control at the beginning of the active document.Add the following method to the
ThisAddInclass. This method initializes the objects needed to create and send a request to the Web service.Create an event handler to retrieve the MSDN Library document about content controls when a user clicks inside of the content control and bind the data to the content control.
Call the
AddRichTextControlAtRangeandInitializeServiceObjectsmethods from theThisAddIn_Startupmethod. For C# programmers, add an event handler.
When you open Word, the RichTextContentControl control appears. The text in the control changes when you click inside it.
To test the VSTO Add-in
Press F5.
Click inside of the content control.
Information is downloaded from the MTPS Content Service and displayed inside the content control.