Display a shared calendar of a recipient

This example shows how to display a recipient's shared calendar by using the CreateRecipient(String) and GetSharedDefaultFolder(Recipient, OlDefaultFolders) methods.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

Sendable items such as MailItem objects always expose the Recipients property which, in turn, enables you to access the Recipients collection for the sendable item. To create a Recipient object that is not bound to the Recipients collection of an item, use the CreateRecipient(String) method of the NameSpace object. Then pass this unbound Recipient object to the GetSharedDefaultFolder(Recipient, OlDefaultFolders) method, which returns a shared Exchange folder. You can then open the shared Exchange folder and display that folder in an explorer window. GetSharedDefaultFolder is used in Exchange delegate scenarios where the delegate has permission to access the folder of the delegator. Before you pass the Recipient object to the GetSharedDefaultFolder method, you must resolve it. To resolve a Recipient object, call its Resolve() method.

In the following code example, DisplayManagerCalendar opens and displays the Calendar folder of the current user’s manager by calling CreateRecipient and GetSharedDefaultFolder. An alert dialog box is displayed if the user does not have permission to open the manager’s Calendar folder or an error occurs.

Note

When you create a Recipient object by using the CreateRecipient method of the Namespace object or the Add(String) method of the Recipients collection, you must provide a recipient name. The Recipient is then resolved against this name. A recipient name can take any of the following formats:

  • Display name
  • Alias
  • Simple Mail Transfer Protocol (SMTP) address

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void DisplayManagerCalendar()
{
    Outlook.AddressEntry addrEntry =
        Application.Session.CurrentUser.AddressEntry;
    if (addrEntry.Type == "EX")
    {
        Outlook.ExchangeUser manager =
            Application.Session.CurrentUser.
            AddressEntry.GetExchangeUser().GetExchangeUserManager();
        if (manager != null)
        {
            Outlook.Recipient recip =
                Application.Session.CreateRecipient(manager.Name);
            if (recip.Resolve())
            {
                try
                {
                    Outlook.Folder folder =
                        Application.Session.GetSharedDefaultFolder(
                        recip, Outlook.OlDefaultFolders.olFolderCalendar)
                        as Outlook.Folder;
                    folder.Display();
                }
                catch
                {
                    MessageBox.Show("Could not open manager's calendar.",
                        "GetSharedDefaultFolder Example",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }
    }
}

See also