LinqPad script that queries public folders and shows the calendar items on the 1st calendar public folder it finds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Statements"> | |
<Reference><RuntimeDirectory>\Microsoft.VisualBasic.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference> | |
<NuGetReference>Microsoft.Exchange.WebServices</NuGetReference> | |
<NuGetReference>Microsoft.IdentityModel.Clients.ActiveDirectory</NuGetReference> | |
<Namespace>Microsoft.Exchange.WebServices.Auth.Validation</Namespace> | |
<Namespace>Microsoft.Exchange.WebServices.Autodiscover</Namespace> | |
<Namespace>Microsoft.Exchange.WebServices.Data</Namespace> | |
<Namespace>Microsoft.IdentityModel.Clients.ActiveDirectory</Namespace> | |
<Namespace>Microsoft.IdentityModel.Clients.ActiveDirectory.Internal</Namespace> | |
<Namespace>Microsoft.IdentityModel.Clients.ActiveDirectory.Native</Namespace> | |
<Namespace>System.Net</Namespace> | |
</Query> | |
var usingFiddler = Process.GetProcessesByName("fiddler").Any(); | |
var webProxy = usingFiddler ? new WebProxy(new Uri("http://localhost:8888"), false) : null; | |
var promptBehavior = PromptBehavior.RefreshSession; | |
var ac = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize"); | |
var ar = ac.AcquireTokenAsync("https://outlook.office365.com", "0e4bf2e2-aa7d-46e8-aa12-263adeb3a62b", new Uri("https://microsoft.com/EwsEditor"), new PlatformParameters(promptBehavior)).Result; | |
var authHeader = ar.CreateAuthorizationHeader(); | |
var mailboxId = ar.UserInfo.DisplayableId; | |
var exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1, TimeZoneInfo.Utc); | |
exchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"); | |
exchangeService.HttpHeaders.Add("Authorization", authHeader); | |
exchangeService.HttpHeaders.Add("X-AnchorMailbox", mailboxId); | |
exchangeService.WebProxy = webProxy; | |
var folders = exchangeService.FindFolders( | |
WellKnownFolderName.PublicFoldersRoot, | |
new FolderView(1000)); | |
folders.Select(x => new { x.DisplayName, x.Id.UniqueId }).Dump("Folders"); | |
var publicFolderCalendar = folders.FirstOrDefault(f => f.FolderClass == "IPF.Appointment"); | |
if (publicFolderCalendar != null) | |
{ | |
var cal = CalendarFolder.Bind(exchangeService, publicFolderCalendar.Id); | |
var appointments = cal.FindAppointments(new CalendarView(DateTime.Now.AddYears(-1), DateTime.Now)); | |
appointments.Select(x => new { x.Subject, x.Id.UniqueId }).Dump(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment