New 20Microsoft 20Office 20Outlook 202007 20Programmability 20 20HOL251

Document Sample
New 20Microsoft 20Office 20Outlook 202007 20Programmability 20 20HOL251 Powered By Docstoc
					Hands-on Lab Overview

  1. Introduction

HOL251: New Microsoft® Office Outlook® 2007 Programmability

  2. Technologies Highlighted

        Microsoft Office Outlook 2007

  3. Audience

        Microsoft Office Outlook 2007 developers

  4. Scenario

        Microsoft® Office Outlook® 2007 introduces a significant number of programmability features. With earlier versions of Microsoft
        Office Outlook, a solutions developer would often require the use of CDO, Exchange Client Extensions, or Extended MAPI (in addition
        to the Microsoft Office Outlook object model) to create a complete solution. With Office Outlook 2007, the need for technologies
        outside of the Microsoft Office Outlook Object Model has been virtually eliminated.

        Office Outlook 2007 adds a number of new object model features, including:

               Comprehensive Views objects.
               Enhanced Events.
               Context Menu customization.
               Rules objects.
               NavigationPane objects.
               ExchangeUser, ExchangeDistributionList, and SelectNamesDialog objects.
               StorageItem object for add-in storage.
               Fast, read-only access for enumeration, sort, and search.
               Customizable Outlook Form Regions.

        In Exercise 1, the developer will create a simple custom add-in that programmatically displays the address book dialog box when a user
        clicks a toolbar button and saves state information regarding the user’s decision. In this exercise, you will use the new
        SelectNamesDialog object and the new StorageItem object.
                                                                       1
      In Exercise 2, the developer will create an Office Outlook 2007 add-in and be introduced to additional new features of the Office
      Outlook 2007 object model, specifically regarding task pane creation, search functionality, and Exchange user information. The
      application provides assistance to the user in the form of a task pane that displays information about messages, appointments, and
      contacts that helps a user when planning a meeting.

      In Exercise 3, the developer will modify an existing Office Outlook 2007 form region and an Office Outlook 2007 add-in that contains
      custom code that loads the form region in Outlook. The form region is for the RSS article display within Outlook.

5. Purpose of Hands-on Lab

      The objective of this lab is to introduce you to some of the new programmability features that Microsoft Office Outlook 2007 provides.
      The following exercises will demonstrate programmatically extending Office Outlook 2007 to provide custom functionality.


6. Lab Notes

     All exercises use the new Code Snippet technology in Microsoft Visual Studio® 2005 to make lab code easily accessible for your
     convenience. Although using the snippets may simplify completing the exercises, you have the option of typing the code listed in the lab
     steps for a more realistic experience. In the code examples all code to be added by you will have an example shown in context with the
     added code appearing in bold.

     You can insert code snippets several different ways in Visual Studio 2005. If you are not familiar with them, the lab machine simplifies
     it even further by adding the Insert Snippet command on the menu bar. Simply place the cursor where you are directed to insert the
     code and click Insert Snippet. Then, navigate to the appropriate snippet as directed by the lab instructions.

     For example, for the first code snippet used in Exercise 1, you should select CustomizingUI, followed by Exercise1, then Code1-
     UsingStatements.




                                                                    2
Figure 2.1. Inserting a code snippet

If you are having problems with the lab build process there are “complete” snippets provided for each file that is modified during this
lab. Using the appropriate “compete” snippet will put the files into their final state. To use these files, first ensure that you are viewing
the code in the Development pane, then press Ctrl-A to select all text. Press the Insert Snippet button and choose the snippet hierarchy
corresponding to the proper lab and exercise. You should then see a snippet listed as CodeComplete-Filename where Filename would
be the file you are currently viewing.

This lab will use several common terms to refer to different tool panes and sections of Visual Studio 2005. Below is a diagram of the
Visual Studio application that has the positioning of the areas called out for easy reference.




                                                                3
           1.   Development pane
           2.   Solution Explorer
           3.   Properties Window
           4.   Toolbox
           5.   Insert Snippet button



Hands-on Lab

Discussion Points                                                               Actions taken
Exercise 1 – Creating a Simple Office Outlook 2007 Add-In

One of the key benefits of the Microsoft Office Outlook 2007 object model is
the unlimited potential for custom solutions. Developers can use Office
Outlook 2007 to create custom applications that extend the familiar interface
of Microsoft Office Outlook.


                                                                                4
In this exercise, you will create a simple custom add-in that programmatically
displays the address book dialog box when a user clicks a toolbar button and
saves state information regarding the user‟s decision. In this exercise, you
will use the new SelectNamesDialog object and the new StorageItem object.

While logged on as NicoleH, you are going to begin by creating a new Visual            Login using the following credentials:
Studio 2005 project.                                                                             a. Username: nicoleh
                                                                                                 b. Password: pass@word1
                                                                                                 c. Log on to: LITWAREINC
                                                                                       Wait for the Warmup Script to complete and the popup window to close.
                                                                                       Start Microsoft Visual Studio 2005.
                                                                                       Select File | New | Project.
                                                                                       On the left side in the Project types box select Visual C# | Office | 2007 Add-
                                                                                        ins.
                                                                                       On the right side in the Templates box select Outlook Add-in.
                                                                                       For the Project Name, type ShowAddressBook.
                                                                                       For the Location, type C:\HOL\OutlookProgrammability.
                                                                                       Click Add.
Add a new class to the project named CustomOutlookUI.cs.                               Select Project | Add Class…
                                                                                       On the right side in the Categories box, select Visual C# Project Items
                                                                                       On the left side in the Templates box, select Class
                                                                                       In the Name textbox, type CustomOutlookUI.cs
                                                                                       Click OK button
The 2007 Microsoft Office system includes the Microsoft.Office.Core                    Insert code snippet OutlookProgrammability > Exercise1 > Code1-
namespace, which simplifies working with Microsoft Office system objects                Namepaces or type the following code at the beginning of the project in the
programmatically from Visual Studio. Add a reference to the                             “Using” section.
Microsoft.Office.Core namespace and the Microsoft.Office.Interop
namespace by adding the following code just after the “Using System” line of                using System;
code.                                                                                       using System.Collections.Generic;
                                                                                            using System.Text;
The System.Windows.Forms namespaces is used for the MessageBox
control used in the application. System.Runtime.InteropServices is for                      using   System.Runtime.InteropServices;
attributes needed to interface with the unmanaged code of Outlook.                          using   System.Windows.Forms;
                                                                                            using   Outlook = Microsoft.Office.Interop.Outlook;
                                                                                            using   Office = Microsoft.Office.Core;

Declare the class as public and add interop attributes so that the class can be        Find the class declaration line. It looks as below.
properly identified in unmanaged code.                                                       class CustomOutlookUI
                                                                                       On the class declaration line type public and add a space before the existing
                                                                                        text.
                                                                                       Above the class declaration line insert code snippet OutlookProgrammability
                                                                                        > Exercise1 > Code2-ClassAttributes.
                                                                                       The ComVisible attribute set to true must be added in order for the class to be
                                                                                        seen by the unmanaged code of Outlook.
                                                                                  5
                                                                                      The ProgId attribute is used to manually assign a ProgId to a class. If none is
                                                                                       specified it will default to the fully qualified name of the class, since this value
                                                                                       will be later in the code it is assigned here for clarity.
                                                                                      The end result of additions will look as below with the changes in bold.
                                                                                            [ComVisible(true)]
                                                                                            [ProgId(“ShowAddressBook.CustomOutlookUI”)]
                                                                                            public class CustomOutlookUI
First you will declare the global variables and constructor of class.                 Just inside of the classes opening curly bracket insert code snippet
                                                                                       OutlookProgrammability > Exercise1 > Code3-Declarations and
                                                                                       Constructor or type the following code.

                                                                                           public class CustomOutlookUI
                                                                                           {
                                                                                               private Office.CommandBarButton toolbarButton;
                                                                                               private Outlook.Application application;

                                                                                               public CustomOutlookUI(Outlook.Application
                                                                                           application)
                                                                                               {
                                                                                                   this.application = application;
                                                                                               }
                                                                                           }

Add an Init that will be call during start up of Outlook. The Init method will        After the constructor, insert code snippet OutlookProgrammability >
add a button to the standard toolbar using the CommandBars object.                     Exercise1 > Code4-Init or type the following.

                                                                                           public void Init()
                                                                                           {
                                                                                               Office.CommandBars commandBars =
                                                                                           applicationObject.ActiveExplorer().CommandBars;

                                                                                                 try
                                                                                                 {
                                                                                                   // See if the button already exists
                                                                                                   this.toolbarButton =
                                                                                           (Office.CommandBarButton)commandBars["Standard"].Control
                                                                                           s["Show Address Book"];
                                                                                               }
                                                                                               catch (System.Exception)
                                                                                               {
                                                                                                   // Create it if it doesn't exist
                                                                                                   this.toolbarButton =
                                                                                           (Office.CommandBarButton)commandBars["Standard"].Control
                                                                                           s.Add(1, System.Reflection.Missing.Value,
                                                                                           System.Reflection.Missing.Value, 5,

                                                                                 6
                                                                                           System.Reflection.Missing.Value);
                                                                                                       this.toolbarButton.Caption = "Show Address
                                                                                           Book";
                                                                                                       this.toolbarButton.Style =
                                                                                           Office.MsoButtonStyle.msoButtonIconAndCaption;
                                                                                                  }
                                                                                                  this.toolbarButton.BeginGroup = true;
                                                                                                  this.toolbarButton.FaceId = 731;
                                                                                                  this.toolbarButton.Tag = "Show Address Book";
                                                                                                  // pass in the ProgId
                                                                                                  this.toolbarButton.OnAction =
                                                                                           "!<ShowAddressBook.CustomOutlookUI>";
                                                                                                  this.toolbarButton.Visible = true;
                                                                                                  // hook up the button’s click even to
                                                                                           OnToolbarButtonClick
                                                                                                  this.toolbarButton.Click += new
                                                                                           Microsoft.Office.Core._CommandBarButtonEvents_ClickEvent
                                                                                           Handler(this.OnToolbarButtonClick);
                                                                                                  // render the button
                                                                                                  this.toolbarButton.PasteFace();
                                                                                           }
You will add a Shutdown method that will remove the toolbar and be ran                After the Init method insert the snippet OutlookProgrammability > Exercise1
while Outlook is closing.                                                              > Code5-Shutdown
                                                                                           public void Shutdown()
                                                                                           {
                                                                                           this.toolbarButton.Delete(System.Reflection.Missing.Valu
                                                                                           e);
                                                                                                  this.toolbarButton = null;
                                                                                           }
Add the OnToolbarButtonClick method that will receive the click event of the          After the Shutdown method insert code snippet OutlookProgrammability >
toolbarButton object.                                                                  Exercise1 > Code6-OnToolbarButtonClick or type the following code.

In the first portion of the method you will display the Office Outlook 2007                public void OnToolbarButtonClick(Office.CommandBarButton
address book using the new Office Outlook 2007 SelectNamesDialog                           cmdBarButton, ref bool cancel)
method.                                                                                    {
                                                                                               Outlook.SelectNamesDialog snd =
In the second portion of the method, you will be introduced to an additional               application.Session.SelectNamesDialog;
new feature of the Office Outlook 2007 object model, which enables you to                      snd.Caption = "Custom Address Book Dialog";
                                                                                               snd.AllowMultipleSelection = true;
store application-level data in hidden Office Outlook 2007 items.
                                                                                               snd.Display();
                                                                                               MessageBox.Show("You selected " +
Traditionally, developers who wanted to store private application date                     snd.Recipients.Count + " recipient(s).",
resorted to CDO‟s HiddenMessages collection. Office Outlook 2007 provides                  "ShowAddressBook");
a StorageItem object, which provides an easy way to store application data
in hidden items. A hidden item is just like a regular Microsoft Office Outlook                  // Save the information in the StorageItem object
item, such as an e-mail message, except that it is not visible within
                                                                                 7
applications such as Office Outlook 2007 or Microsoft Office Outlook Web                   Outlook.Folder fldr =
Access. Office Outlook 2007 will create as many hidden items as you need –             (Outlook.Folder)applicationObject.Session.GetDefaultFold
simply specify a different subject line for each StorageItem. Be careful:              er(Outlook.OlDefaultFolders.olFolderInbox);
because the items are stored by folder, those items can be updated by                      Outlook.StorageItem storageItem =
anyone with access to that folder. Custom values can be stored in the                  fldr.GetStorage("RecipientCount",
message body, attachments, or custom Office Outlook 2007 properties of the             Outlook.OlStorageIdentifierType.olIdentifyBySubject);
item.
                                                                                           // Get existing storage item from last save
                                                                                           string recipCount = storageItem.Body;
                                                                                           System.Windows.Forms.MessageBox.Show("You selected "
                                                                                       + recipCount + " recipient(s) last time.", "History");

                                                                                             // Save current information in storage item
                                                                                             storageItem.Body = snd.Recipients.Count.ToString();
                                                                                             storageItem.Save();
                                                                                       }
Now that the CustomOutlookUI class has been completed, it needs to                Scroll up to the top of the file and just inside of the opening bracket of the
hooked up to Outlook. You will implement a partial ThisAddIn class which           namespace insert code snippet OutlookProgrammability > Exercise1 >
will override the OnStartup method. Inside the OnStartup method the Init           Code7-ThisAddIn or type the following code.
method of an instance of CustomOutlookUI class will be called. An event           The end result will look as follows with the additions in bold
handler will be registered for the Shutdown event of Outlook. During the
Shutdown event, the Shutdown method of CustomOutlook will be called.                   namespace ShowAddressBook
                                                                                       {
                                                                                           public partial class ThisAddIn
                                                                                           {
                                                                                               private CustomOutlookUI cOutlookUI = null;

                                                                                               public override void OnStartup()
                                                                                               {
                                                                                                   if (cOutlookUI == null)
                                                                                                   {
                                                                                                       cOutlookUI = new
                                                                                       CustomOutlookUI(this.Application);
                                                                                                   }
                                                                                                   cOutlookUI.Init();
                                                                                                   this.Shutdown += new
                                                                                       EventHandler(this.Application_Shutdown);
                                                                                                   base.OnStartup()
                                                                                               }

                                                                                               private void Application_Shutdown(object sender,
                                                                                       EventArgs eArgs)
                                                                                               {
                                                                                                   if(cOutlookUI != null)
                                                                                                   {
                                                                                                        cOutlookUI.Shutdown();
                                                                             8
                                                                                                                 cOutlookUI = null;
                                                                                                            }
                                                                                                       }
                                                                                                  }

                                                                                                  [ComVisible(true)]
                                                                                                  [ProgId(“ShowAddressBook.CustomOutlookUI”)]
                                                                                                    public class CustomOutlookUI
To see the application in action, simply build the solution and then run the            Close Office Outlook 2007 if it is running.
installation project that was added for you by the add-in wizard.                       Press CTRL+SHIFT+S to save the solution.
                                                                                        Select the Build | Build Solution menu command.
                                                                                        Right-click the ShowAddressBookSetup project and click Build to build the
                                                                                         setup project.
                                                                                        Note: If prompted to install or configure SQL Server 2005 Tools, cancel to
                                                                                         continue.
                                                                                        After the build process is complete, right-click the ShowAddressBookSetup
                                                                                         project in the Solution Explorer and click Install.
                                                                                                  o When the Setup Wizard begins, click Next.
                                                                                                  o Click Next to accept the project settings.
                                                                                                  o Click Next to begin the installation.
                                                                                                  o Click Close to complete the wizard.
                                                                                        Navigate to Start | All Programs | Microsoft Office | Microsoft Office
                                                                                         Outlook 2007.
                                                                                        Note the new Show Address Book button. Click Show Address Book to see
                                                                                         your address book (if you don‟t see names, choose moss from the Address
                                                                                         Book drop down menu).
                                                                                        Holding down CTRL, select several names from the address book and click To.
                                                                                        Click OK.
                                                                                        Your code will display two message boxes:
                                                                                                  o The first displays the number of currently selected recipients.
                                                                                                  o The second displays the number of recipients you selected last
                                                                                                      time.
                                                                                        Repeat the previous four steps (starting with Click Show Address Book to see
                                                                                         your address book) again to confirm that the number of selected items was
                                                                                         stored and can be retrieved.
                                                                                        Close Office Outlook 2007.
                                                                                        Right-click the ShowAddressBookSetup project and click Uninstall.
                                                                                        Close Visual Studio 2005.

Exercise 2 – Creating a Custom Office Outlook 2007 Task Pane

In this lab exercise, you will be introduced to additional new features of the
Office Outlook 2007 object model regarding task pane creation, search
functionality, and Exchange user information.
                                                                                 9
About the Add-In – “Prepare Me”

The application you will be developing in the lab is a .NET-based Office
Outlook 2007 add-in that provides assistance to the user in the form of a task
pane. The pane displays additional information about messages,
appointments, and contacts that helps a user when planning a meeting.

You are going to begin by opening an existing Visual Studio 2005 solution.                Start Microsoft Visual Studio 2005.
The solution contains three projects:                                                     Select File | Open | Project/Solution.
                                                                                          Navigate to C:\HOL\OutlookProgrammability\Exercise2\PrepareMeAddin.
  PrepareMeAddin – an add-in project that will get loaded by Office                      Select PrepareMeAddin.sln.
   Outlook 2007, much like the one you created in Exercise 1.                             Click Open.
  PrepareMeControl – a .NET control that gets loaded by the                              From the Visual Studio Solution Explorer, right-click CustomOutlookUI.cs and
   PrepareMeAddin and serves as an Office Outlook 2007 task pane user                      click View Code.
   interface.
  PrepareMeAddinSetup – the setup project for the add-in.

To get started with the sample application in this exercise, you need to                Insert code snippet OutlookProgrammability > Exercise2 > Code1-
include the references to the Microsoft.Office.Interop.Outlook namespace                 Namepaces or type the following code below the Code1-Namepaces comment
and to Microsoft.Office.Core. Note that Microsoft.Office.Interop.Outlook is              in the „Using‟ section at the start of the CustomOutlookUI.cs file.
included to access the Microsoft Office Outlook object model, while
Microsoft.Office.Core is included to use Microsoft Office system toolbar                       // Insert OutlookProgrammability > Exercise2 > Code1-
buttons.                                                                                       Namepaces
                                                                                               using Outlook = Microsoft.Office.Interop.Outlook;
                                                                                               using Office = Microsoft.Office.Core;

Office Outlook 2007 enables you to create a customizable task pane for                  Expand the Event Handlers region if not already done
Inspectors (UI views of individual items) and Explorers (UI views of the                Insert code snippet OutlookProgrammability > Exercise2 > Code2-
overall structure of Office Outlook 2007). The task pane is dockable, either on          AddTaskPane or type the following code below the Code2-AddTaskPane
the side or the bottom of the screen. If more than one task pane is loaded, all          comment in the CustomOutlookUI.cs file, inside the
task panes are visible within Office Outlook 2007.                                       Inspectors_NewInspector() event procedure, just after the second if
                                                                                         statement.
In this exercise, you will create an add-in that loads a task pane that helps a
user prepare for meetings. The add-in will create a button that shows up on                    // Insert OutlookProgrammability > Exercise2 > Code2-
the ribbon when a user has an Appointment or Meeting inspector open.                           AddTaskPane
When the user clicks the button, a task pane is displayed that provides the                    // Get a reference to the current appointment item
user with information regarding the current meeting‟s attendees (including                     Outlook.AppointmentItem item =
organizational details), related appointments, and other items with the same                   (Outlook.AppointmentItem)olItem.InnerObject;
category.
                                                                                               // Add the custom task pane to the inspector window
                                                                                               Office.CustomTaskPane taskPane =
To create a task pane, you simply create a .NET control and then load it
                                                                                               m_CtpFactory.CreateCTP(TASK_PANE_PROG_ID, "Meeting
through the Office Outlook 2007 object model. For this exercise, a task pane                   Planner", Inspector);
                                                                                  10
control has been created for you.

You will now add the following code to the PrepareMeAddin project in the
Connect class, which will load the task pane control when an Office Outlook
2007 AppointmentItem inspector is opened.

To view the user interface design for the task pane control, double-click the           Right-click on TaskPaneControl.cs in the Solution Explorer and choose View
TaskPaneControl.cs class under the PrepareMeControl project in the                       Code
Solution Explorer. Right-click the TaskPaneControl.cs file and select View              Expand the Methods region if not done
Code.                                                                                   Insert code snippet OutlookProgrammability > Exercise2 > Code3-
                                                                                         OutlookFastAccessTable or type the following code below the Code3-
Once the task pane is loaded, it needs to look at the current appointment and            OutlookFastAccessTable comment inside the LoadMeetings procedure, just
load items with the same categorization. In older versions of the Microsoft              after the Meetings.Notes.Clear code within the TaskPaneControl.cs file.
Office Outlook object model, iterating through large collections was relatively
slow, which caused many developers to use alternative methods (for                           // Insert OutlookProgrammability > Exercise2 > Code3-
example, CDO or Extended MAPI) to access items quickly. Office Outlook                       OutlookFastAccessTable
2007 provides a new table that provides lightweight read-only access to items                // Search for similar meetings given the subject of the
for enumeration, sort, and search at speeds up to ten times faster than                      current meeting
before.
                                                                                             // Get the user’s Calendar folder
                                                                                             Outlook.Folder calendar =
In this section, you‟ll add code to access items through the fast access table.
                                                                                             (Outlook.Folder)m_Application.Session.GetDefaultFolder(O
                                                                                             utlook.OlDefaultFolders.olFolderCalendar);

                                                                                             // Build a filter string for use by the Outlook fast
                                                                                             access table
                                                                                             string filter =
                                                                                             HelperFunctions.BuildFilterForSubjectSearch(m_Appointmen
                                                                                             t.Subject);

                                                                                             // Create a fast-access read-only table for the filtered
                                                                                             calendar view
                                                                                             Outlook.Table tbl = calendar.GetTable(filter, false);

In Office Outlook 2007, you are now able to drive Search programmatically               Insert code snippet OutlookProgrammability > Exercise2 > Code4-
through the object model. In this section, you will create a new method called           ExplorerSearch or type the following code below the Code4-ExplorerSearch
DisplaySearch that accepts a single parameter that represents the search                 comment, immediately before the DisplaySearchForCategory() procedure
string. The search string is typically in the form of “property: value”.                 within the TaskPaneControl.cs file.

                                                                                             // Insert OutlookProgrammability > Exercise2 > Code4-
                                                                                             ExplorerSearch
                                                                                             private void DisplaySearch(string searchSyntax)
                                                                                             {
                                                                                                 // Get a reference to the Inbox
                                                                                                 Outlook.Folder folder =
                                                                                  11
                                                                                           (Outlook.Folder)m_Application.Session.GetDefaultFolder(O
                                                                                           utlook.OlDefaultFolders.olFolderInbox);

                                                                                               // Create a new explorer that displays the Inbox
                                                                                               Outlook.Explorer newExplorer =
                                                                                           m_Application.Explorers.Add(folder,
                                                                                           Outlook.OlFolderDisplayMode.olFolderDisplayNormal);

                                                                                               // Create and display a search result using the
                                                                                           Explorer window
                                                                                               newExplorer.Search(searchSyntax, true);

                                                                                           newExplorer.ShowPane(Outlook.OlPane.olNavigationPane,
                                                                                           false);
                                                                                               newExplorer.Display();
                                                                                           }

The Office Outlook 2007 object model provides enhancements to the                      Insert code snippet OutlookProgrammability > Exercise2 > Code5-
AddressEntry object, along with a new ExchangeUser object. This                         ExchangeUser or type the following code below the Code5-ExchangeUser
combination provides access to property values for Exchange-based                       comment in the MakeRecipientNode(Outlook.Recipient) procedure within the
recipient objects. In this section, you will access properties of a user using          TaskPaneControl.cs file.
the ExchangeUser object so that you can display those properties in the task
pane.                                                                                      // Insert OutlookProgrammability > Exercise2 > Code5-
                                                                                           ExchangeUser
Note: For performance reasons, Microsoft Exchange Server is often not                      if (recipAddrEntry.AddressEntryUserType ==
deployed in the Hands-on lab environment. If Exchange is unavailable, this                 Outlook.OlAddressEntryUserType.olExchangeUserAddressEntr
functionality will not be visible in the final task pane.                                  y)
                                                                                           {
                                                                                               // Get the corresponding Exchange User object
                                                                                               Outlook.ExchangeUser exchUser =
                                                                                           recipAddrEntry.GetExchangeUser();

                                                                                                // Extract the name of the user and his/her job
                                                                                           title
                                                                                                node.Text = exchUser.Name + ", " +
                                                                                           exchUser.JobTitle;
                                                                                                exchUser = null;
                                                                                           }
                                                                                           else
                                                                                           {
                                                                                                node.Text = recipAddrEntry.Name;
                                                                                           }

To see the application in action, simply build the solution and then run the           Close Office Outlook 2007 if it is running.
installation project that was added for you by the add-in wizard.                      Select the Build | Build Solution menu command.

                                                                                 12
                                                                                   After the build process is complete, right-click the PrepareMeAddInSetup
                                                                                    project and click Install.
                                                                                             o If Install menu option is not available, first select Build. After the
                                                                                                  build is finished, select Install.
                                                                                             o When the Setup Wizard begins, click Next.
                                                                                             o Click Next to accept the project settings.
                                                                                             o Click Next to begin the installation.
                                                                                             o Click Close to complete the wizard.
                                                                                   Select the Debug | Start Debugging menu command. Outlook will open.
                                                                                   Navigate to the Calendar
                                                                                   Right-click inside the calendar and select New Appointment
                                                                                             o .Click Invite Attendees
                                                                                                       To: luisb@litwareinc.com; daveb@litwareinc.com
                                                                                                       Subject: Meeting
                                                                                                       Location: Office
                                                                                                       Click Send
                                                                                   Double-click on the appointment you just created.
                                                                                   Click the Meeting Planner button on the ribbon to see the newly created task
                                                                                    pane with additional information about the appointment.
                                                                                             o You obtained the attendee titles for each meeting recipient by using
                                                                                                  the ExchangeUser object. (Note: Titles are obtained only when
                                                                                                  connected to Exchange)
                                                                                             o Right-click Dave Barnett and click Display All Messages. You will
                                                                                                  see a search for all messages that are in the Inbox from this user.
                                                                                                  You achieved this by using the Explorer.Search method.
                                                                                   Close all windows and applications

Exercise 3 – Creating a Custom Outlook 2007 Form Region

In this lab exercise, you will be introduced to designing form regions in
Outlook 2007. You will also create

About the Add-In – “Prepare Me”

The application you will be developing in the lab is a .NET-based Office
Outlook 2007 add-in that adds the custom form region for RSS articles. The
form region displays RSS articles in a web browser and provides additional
functionality.


Design a form region.                                                              Open Outlook by using Start | Microsoft Office | Microsoft Office Outlook
                                                                                    2007
                                                                                   Open the Design Form dialog by using Tools | Forms | Design a Form

                                                                             13
      In the Design Form dialog
                o In the Look In drop down, select Standard Forms Library
                o In the list select RSS Article <Hidden>
                o [Optional] Show the advanced properties of the form by click the
                    Advanced >> button. The advanced properties are shown at the
                    bottom of the dialog. The message class is particularly important
                    as it is used to determine what registry keys to register for form
                    regions as you learn later in the exercise.
                o Click Open button
      After the forms designer opens, click on the Form Region button in the Design
       group and select Open Form Region…
      Navigate to the directory
       C:\HOL\OutlookProgrammability\Exercise3\RssMessages\RssMessages\R
       ssMessages, select Rss Form Region.ofs and click Open button.
      Click on the Control Toolbox button (the small button with the crossed wrench
       and hammer) in Tools group




      Right click the Toolbox dialog and select Custom Controls…




      In the Additional Controls dialog
               o Unselect everything in the Available Controls list and then select

14
                    Microsoft Office Outlook Command Button Control
                o Click OK button
      Drag the OlkCommandButton control from the toolbox to the right of the
       Refresh button
      Right click on the new OlkCommandButton1 control and select Properties




      On the Display tab enter the following values
              o Name – reloadPostButton
              o Caption – Reload Post
                       Note: Putting an ampersand (&) in front of any of the letter
                          gives the button a quick key.




15
      Save the form region by clicking the Form Region button in the Design group
       and selecting Save Form Region




16
                                                                                   Close the form designer window and Outlook
You will now open the RssMessages project in Visual Studio 2005.                   Open Windows Explorer by using Start | Windows Explorer
                                                                                   Navigate to
                                                                                    C:\HOL\OutlookProgrammability\Exercise3\RssMessages\RssMessages
                                                                                   Double click RssMessages.sln
                                                                                   Visual Studio 2005 will open with the RssMessages project loaded
Analyze the form manifest.                                                         To view either RSSFormRegion.xml or PreviewRSSFormRegion.xml, double-
                                                                                    click the file in the Solution Explorer.
Form manifests detail how a form is to be used by Outlook.

The basic element definitions are:
    name – defines the internal name used by the add-in to refer to this
       form region.
    title - defines the display text used in the adjoining region header.
    formRegionType - defines the type of region, in this case an
       adjoining region.
    showInspectorRead - defines whether this region will be shown for
       read inspectors.
    showReadingPane - defines whether this region will be displayed in
       the Reading Pane.
    showInspectorCompose - defines whether this region will be
       displayed for compose inspectors.
    addin - indicates the ProgID of the add-in that this form region is
       linked with.

For more information about form region manifest download the Office

                                                                             17
schema reference from this web site:
http://www.microsoft.com/downloads/details.aspx?familyid=15805380-f2c0-
4b80-9ad1-2cb0c300aef9&displaylang=en

The RSS form region will be displayed in both the Preview pane (inside
Outlook) and in the Reading pane (when a RSS article is opened in a
separate reading window). In the Preview pane it will be displayed as an
adjoining region and in the Reading pane it will be will be displayed as a
separate region. Due to this design choice there are two XML manifests
(RSSFormRegion.xml [reading] and PreviewRSSFormRegion.xml [preview])
needed to describe how the form region is used in the individual panes.

Modify FormRegionManager class to implement the _FormRegionStartup                   In Solution Explorer, double-click FormRegionManager.cs.
interface.                                                                           On the class declaration add the following after Disposable
                                                                                           , Outlook._FormRegionStartup
                                                                                     The end result of changes will look as follows
                                                                                           internal sealed class FormRegionManager : IDisposable,
                                                                                           Outlook._FormRegionStartup
                                                                                     To add the methods needed to implement the _FormRegionStartup interface
                                                                                      right-click the text _FormRegionStartup and select Implement Interface |
                                                                                      Implement Interface.
Modify the BeforeFormRegionShow method to not throw an exception.                    Scroll down to find BeforeFormRegionShow that was added when clicking the
                                                                                      Implement Interface menu item.
The BeforeFormRegionShow method is called by the application before each             Delete the code of the BeforeFormRegionShow method and insert snippet
instance of the form region is shown (there can be multiple instances of the          OutlookProgrammability > Exercise3 > Code1-BeforeFormRegionShow
form region show at any time).                                                       The end result of your changes will look as follows with the additions in bold
                                                                                           public void
The method initializes a new instance of the RegionInstance and adds it to                 BeforeFormRegionShow(Microsoft.Office.Interop.Outlook.Fo
the regionInstances collection. The RegionInstance class represents a                      rmRegion FormRegion)
individual instances of the RSS article form region and handles the state of               {
the form region‟s controls and events. It also adds an event handler for the
OnFormRegionClose event, so that the region can be removed from the                           RegionInstance ri = new RegionInstance(FormRegion);
                                                                                              ri.OnFormRegionClose += new
regionInstances collection.
                                                                                          FormRegionCloseHandler(ri_OnFormRegionClose);
                                                                                              this.regionInstances.Add(FormRegion, ri);

                                                                                          }
Modify the GetFormRegionIcon method to not throw an exception.                       Delete the code of the GetFormRegionIcon method and insert snippet
                                                                                      OutlookProgrammability > Exercise3 > Code2-GetFormRegionIcon
The GetFormRegionIcon method is called by the application to retrieve the            The end result of your changes will look as follows with the additions in bold
icons for the form region. Outlook requires a COM compatible object to be                 public object GetFormRegionIcon(string FormRegionName,
returned.                                                                                 int LCID,
                                                                                          Microsoft.Office.Interop.Outlook.OlFormRegionIcon Icon)
The if statement first checks if the request is for the form region in the                {

                                                                               18
Reading pane (TQForms.RssFormRegion) and if the requested icon is for the
ribbon. If the requirements are met, an icon is retrieved from an embedded                   if (FormRegionName == “TQForms.RssFormRegion”
resource and passed the helper PictureUtils class which returns an                               && Icon ==
IPictureDisp object which is COM compatible.                                             Outlook.OlFormRegionIcon.olFormRegionIconPage)
                                                                                             {
                                                                                                 return
                                                                                         PictureUtils.ToIPictureDisp(Resources.FeedIcon);
                                                                                             }

                                                                                               return null;
                                                                                         }


Modify the GetFormRegionManifest method to not throw an exception.                  Delete the code of the GetFormRegionIcon method and insert snippet
                                                                                     OutlookProgrammability > Exercise3 > Code3-GetFormRegionManifest
The GetFormRegionManifest method is called by the application to get a              The end result of your changes will look as follows with the additions in bold
form region‟s manifest.                                                                  public object GetFormRegionManifest(string
                                                                                         FormRegionName, int LCID)
The added code returns the correct XML manifest from the embedded                        {
resources.                                                                                    if (FormRegionName == “TQForms.RssFormRegion”)
                                                                                              {
                                                                                                    return Resources.RSSFormRegion;
                                                                                              }
                                                                                              else if (FormRegionName ==
                                                                                         “TQForms.PreviewRssFormRegion”)
                                                                                              {
                                                                                                    return Resources.PreviewRSSFormRegion;
                                                                                              }

                                                                                               return null;
                                                                                         }

Modify the GetFormRegionStorage method to not throw an exception.                   Delete the code of the GetFormRegionStorage method and insert snippet
                                                                                     OutlookProgrammability > Exercise3 > Code4-GetFormRegionStorage
The GetFormRegionStorage method is called by the application to retrieve            The end result of your changes will look as follows with the additions in bold
the form region and expects a byte array representation of the form region.              public object GetFormRegionStorage(string
                                                                                         FormRegionName, object Item, int LCID,
The added code returns the Rss Form Region.ofs file that you modified                    Microsoft.Office.Interop.Outlook.OlFormRegionMode
earlier. It was added to the project‟s embedded resources.                               FormRegionMode,
                                                                                         Microsoft.Office.Interop.Outlook.OlFormRegionSize
                                                                                         FormRegionSize)
                                                                                         {
                                                                                              if (FormRegionName == “TQForms.RssFormRegion” ||
                                                                                         FormRegionName == “TQForms.PreviewRssFormRegion”)
                                                                                              {

                                                                              19
                                                                                                 return Resources.Rss_Form_Region;
                                                                                            }

                                                                                             return null;
                                                                                      }
The last addition to the FormRegionManager.cs file will be the partial           Inside of the namespace‟s opening curly bracket insert snippet
ThisAddIn class that will hookup the form region to the add-in.                   OutlookProgrammability > Exercise3 > Code5-ThisAddIn
                                                                                 The end result of your changes will look as follows with the additions in bold
The code overrides the existing RequestService method of ThisAddIn class              namespace RssMessages
and returns an instance of the FormRegionManager class and adds an event              {
handler for the Shutdown event of the class. The Shutdown event handler                      public partial class ThisAddIn
calls the Dispose method of the FormRegionManager and sets it to null.                       {
                                                                                                  private FormRegionManager formRegionManager =
                                                                                      null;

                                                                                              //Override the RequestService call to handle
                                                                                              //requests for the FormRegion service
                                                                                              protected override object RequestService(Guid
                                                                                      serviceGuid)
                                                                                              {
                                                                                                   if (serviceGuid ==
                                                                                      typeof(Outlook._FormRegionStartup).GUID)
                                                                                                   {
                                                                                                       this.Shutdown += new
                                                                                      EventHandler(ThisAddInShutdown);
                                                                                                       this.formRegionManager = new
                                                                                      FormRegionManager();
                                                                                                   }

                                                                                                       return base.RequestService(serviceGuid);
                                                                                                   }

                                                                                               private void ThisAddInShutdown(object sender,
                                                                                      EventArgs eArgs)
                                                                                               {
                                                                                                   if (this.formRegionManager != null)
                                                                                                   {
                                                                                                       this.formRegionManager.Dispose();
                                                                                                       this.formRegionManager = null;
                                                                                                   }
                                                                                               }
                                                                                            }

                                                                                               internal sealed class FormRegionManager :
                                                                                      IDisposable, Outlook._FormRegionStartup
The code for the form region is now completed. You will now build and            Build the project by using Build | Build RssMessages
                                                                           20
deploy the add-in.                                                                       Install the add-in
                                                                                                    o In the Solution Explorer right-click RssMessagesSetup and select
                                                                                                        Install
                                                                                                             If the Install menu item is not available, first select the Build
                                                                                                                 menu item then select the Install menu item after the
                                                                                                                 project is finished building
                                                                                                    o Click Next three times to proceed through the installation wizard
                                                                                                        leaving the defaults for any input controls.
                                                                                                    o Click Close button after the installation completes.
After the add-in installation is completed, the form region must be registered           Navigate in Windows Explorer to
with Outlook. Form regions are registered by adding keys to the registry.                 C:\HOL\OutlookProgrammability\Exercise3\RssMessages\RssMessages.
The registry path is:                                                                    Double click on FormRegionRegistration.reg.
                                                                                         Click Run button
HKEY_CURRENT_USER\Software\Micrsoft\Office\Outlook\FormRegions\[M                        Click Yes button in the Registry Editor dialog
essage Class]                                                                            Click OK button in the Registry Editor dialog
                                                                                         [Optional] If you wish to view the contents of the .reg file drag it from Windows
Message class is the message class of the form region. Earlier on an                      Explorer into Visual Studio.
optional step you could have viewed the form region‟s message class in the
advanced properties of the Design a Form dialog. In this exercise the form
region‟s message class is IPM.RSS.POST.

HKEY_LOCAL_MACHINE can also be used if the form region is to be
available to all users instead of just the current one. The full path of our key
is:

HKEY_CURRENT_USER\Software\Micrsoft\Office\Outlook\FormRegions\IP
M.RSS.POST

For each form region a subkey must be registered. The key name is the
name of the form, the key type is REG_SZ, and the value is =[AddIn Name].
In this exercise two subkeys are added (TQForms.RssFormRegion and
TQForms.PreviewRssFormRegion) with the value =RssMessages.
As the last step of this exercise you will test the new form region in Outlook.          Return to Visual Studio and start debugging Outlook by selecting Debug | Start
                                                                                          Debugging
                                                                                         In the Mail tab, expand the RSS Feeds and select one of the RSS feeds.
                                                                                         In the Document Center select an article.
                                                                                         The new RSS form region is displayed in the Preview pane with the article
                                                                                          loaded.




                                                                                   21
      Double-click the RSS article in the Document Center to launch the Reading
       pane view.
      Click on the View In Browser button in the ribbon.




      The new RSS form region is displayed. Click on the buttons to view the new
       functionality.




22
Conclusion

  I.   Conclusion

       By completing this lab, you have become familiar with the new programmability features that Microsoft Office Outlook 2007 provides.
       You created a custom add-in that programmatically displays the address book dialog box when a user clicks a toolbar button and saves
       state information regarding the user’s decision. You developed a custom add-in that provides assistance to the user in the form of a task
       pane, which displays additional information about messages, appointments, and contacts.

       You also learned how to design an Outlook form region and connect it to an Outlook Add-In. You created an RSS article form region
       that will load the article within Outlook.
                                                                    23
24

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:30
posted:11/11/2011
language:English
pages:24