Skip to content

Instantly share code, notes, and snippets.

@fpintos
Created August 31, 2018 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpintos/938ee14dd0e8a9eca716513b2e2ae83b to your computer and use it in GitHub Desktop.
Save fpintos/938ee14dd0e8a9eca716513b2e2ae83b to your computer and use it in GitHub Desktop.
LinqPad script that queries public folders and shows the calendar items on the 1st calendar public folder it finds.
<Query Kind="Statements">
<Reference>&lt;RuntimeDirectory&gt;\Microsoft.VisualBasic.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\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