In my previous posts, I showed you how to create a custom ribbon and how to create a custom Task Pane using VSTO, but how do you tie these two together? I've created a solution to show you how to do that. It's based on the scenario of a travel agency that books cruises. A custom ribbon called Contoso Travel Agency shows the Top Cruises and from each picture of the cruise in the gallery drop down, the travel agent can bring up a custom task pane populated with all the information about the cruise. The custom task pane is grabbing information from an Access database using scalar queries. Check out the source code here.
TIP: I found that it's difficult to make two projects talk together when you use VSTO. Instead, it's easier when you use one project and instead just add the ribbon files to say, the custom task pane project. Here, I'm doing just that because your custom task pane is usually more involved with perhaps external data connections, scalar queries, web service calls to the Office SharePoint Server Business Data Catalog, and therefore much more involved. It tends to be much easier to add the ribbon component to the custom task pane project than vice versa.
My ribbon.xml file contains a tag for a group called "Top Cruise Ships" which contains a callback called CTPContext. This tag defines that I have a gallery here with a bunch of images and when you click on them, the CTPContext callback is called.
label="Top Cruise Ships">
label ="Great Waves"/>
label ="Clear Waters"/>
label ="Open Oceans"/>
label ="Angel Fish"/>
Here's the code for the callback:
void CTPContext(Office.IRibbonControl control, string id, int index)
Microsoft.Office.Tools.CustomTaskPane ctrlTaskPane =
ctrl_CruiseInfo ctrl = (ctrl_CruiseInfo)ctrlTaskPane.Control;
if (id == "Northwind") ctrl.cruise_ship = "Northwind";
if (id == "Great_Waves") ctrl.cruise_ship = "Great Waves";
if (id == "Seashell") ctrl.cruise_ship = "Seashell";
if (id == "Open_Oceans") ctrl.cruise_ship = "Open Oceans";
if (id == "Angel_Fish") ctrl.cruise_ship = "Angel Fish";
if (id == "Clear_Waters") ctrl.cruise_ship = "Clear Waters";
else ctrl.cruise_ship = "Northwind";
ctrlTaskPane.Visible = true;
What I'm doing here is I'm setting the taskpane variable in the ribbon class file to the custom task pane instantiated in ThisAddin.cs. This is the critical part of tying together the ribbon and the custom task pane. The rest is just doing hook ups and passing the gallery item ID into the task pane code so that the we can then go back and query the Access database.
I did a webcast on this which involved an Outlook Form Region as well so if you want to see it in action, take a look here.