Home > Back-end >  How to access shared calendars in c# Outlook?
How to access shared calendars in c# Outlook?

Time:03-02

I have troubles with accessing via c# to shared calendars in Outlook. I tried the code bellow. But also tried to iterate over all folder without success...

using Outlook = Microsoft.Office.Interop.Outlook;
using OutlookApp = Microsoft.Office.Interop.Outlook.Application;
        
void test(){
    OutlookApp outlookApp = new OutlookApp();
    NameSpace mapiNamespace = outlookApp.GetNamespace("MAPI");
        
    //Access my calendar is OK!
    MAPIFolder ownerFolder = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
    Console.WriteLine("My: count:"   ownerFolder.Folders.Count   " path:\""   ownerFolder.FullFolderPath   "\" items:"   ownerFolder.Items.Count);
        
    //ownerFolder.Folders.Count == 0, why?
    foreach (MAPIFolder subFolder in ownerFolder.Folders) Console.WriteLine(subFolder.FullFolderPath);
        
    //???Points to my calendar???
    Recipient recipient = mapiNamespace.CurrentUser;
    MAPIFolder sharedFolder = mapiNamespace.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderCalendar);
    Console.WriteLine("Shared: count:"   sharedFolder.Folders.Count   " path:\""   sharedFolder.FullFolderPath   "\" items:"   sharedFolder.Items.Count);
        
    //sharedFolder.Folders.Count == 0, why?
    foreach (MAPIFolder subFolder in sharedFolder.Folders) Console.WriteLine(subFolder.FullFolderPath);
}

Thank you in advance for your answer.

CodePudding user response:

In the following code you pass a local user name to the GetSharedDefaultFolder method:

//???Points to my calendar???
Recipient recipient = mapiNamespace.CurrentUser;
MAPIFolder sharedFolder = mapiNamespace.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderCalendar);

The NameSpace.GetSharedDefaultFolder method returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder). So, you need to pass a user name (or email) of the person who shared the calendar with you, not yours:

Recipient recipient = mapiNamespace.CreateRecipient("Eugene Astafiev");
MAPIFolder sharedFolder = mapiNamespace.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderCalendar);

Note that the Recipient object must be resolved.

Recipient recipient = mapiNamespace.CreateRecipient("Eugene Astafiev");
recipient.Resolve(); 
if(recipient.Resolved)
{ 
   mapiNamespace.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderCalendar);
}

  • Related