Nisan Amzallag
Nisan Amzallag

Reputation: 45

C# - Outlook - get access to a new calendar

I am trying to read all the calendars I have in my outlook with C#, but i have a problem getting access to the ones I create inside outlook (right click -> new calendar).

I'm trying to get them by:

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folderss =   ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

or by:

Application.Session.Stores

but none of them holds my new calendar.

do you have an Idea how to reach them?

Upvotes: 1

Views: 645

Answers (1)

Axel Kemper
Axel Kemper

Reputation: 11322

Calendars are just Folders with DefaultItemType OlItemType.olAppointmentItem.
They can be created in any of the Outlook Stores on any level of the Folder hierarchy.

Assuming that the calendar was created in the root folder of one of your Stores, the following C# code will find it:

 void findMyCalendar(String name)
    {
        string path = null;

        Outlook.Application app = new Outlook.Application();
        Outlook.NameSpace ns = app.GetNamespace("MAPI");

        //  there may be more than one Store
        //  each .ost and .pst file is a Store
        Outlook.Folders folders = ns.Folders;

        foreach (Outlook.Folder folder in folders)
        {
            Outlook.MAPIFolder root = folder;
            path = findCalendar(root, name);

            if (path != null)
            {
                break;
            }
        }

        MessageBox.Show(path ?? "not found!");
    }

//  non-recursive search for just one level
public string findCalendar(MAPIFolder root, string name)
    {
        string path = null;

        foreach (Outlook.MAPIFolder folder in root.Folders) 
        {
            if (folder.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) &&
                (folder.DefaultItemType == OlItemType.olAppointmentItem))
            {
                path = folder.FolderPath;
                break;
            }
        }

        return path;
    } 

Upvotes: 1

Related Questions