Created
March 29, 2021 09:25
-
-
Save BlythMeister/2e46abc94e949c0a556f7c0c7f1607b1 to your computer and use it in GitHub Desktop.
Bank Holidays To Outlook
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="Program"> | |
<NuGetReference>NetOfficeFw.Outlook</NuGetReference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<Namespace>NetOffice</Namespace> | |
<Namespace>NetOffice.OutlookApi</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>System.Net</Namespace> | |
<Namespace>NetOffice.OutlookApi.Enums</Namespace> | |
</Query> | |
Application outlook = new Application(); | |
WebClient client = new WebClient(); | |
bool debugMode = false; | |
void Main() | |
{ | |
if (debugMode == true) | |
{ | |
Console.WriteLine("!!! DEBUG MODE !!!"); | |
} | |
client.Encoding = System.Text.Encoding.UTF8; | |
var bankHolidayJson = client.DownloadString("https://www.gov.uk/bank-holidays.json"); | |
var bankHolidays = JsonConvert.DeserializeObject<BankHolidays>(bankHolidayJson); | |
var calendarItems = GetFuturePublicHolidaysFromCalendar(bankHolidays.EnglandAndWales.Events.Min(x=>x.Date)); | |
foreach (var bankHolidayEvent in bankHolidays.EnglandAndWales.Events) | |
{ | |
var matchingEvent = calendarItems.FirstOrDefault(x=>x.Start.Date == bankHolidayEvent.Date.Date); | |
if(matchingEvent == null) | |
{ | |
AddHolidayToCalendar(bankHolidayEvent); | |
} | |
else if(!matchingEvent.Subject.Equals(bankHolidayEvent.Title)) | |
{ | |
UpdateHolidaySubject(matchingEvent, bankHolidayEvent); | |
} | |
else | |
{ | |
Console.WriteLine($"{matchingEvent.Subject} on {matchingEvent.Start.Date:yyy-MM-dd} is already present and correct"); | |
} | |
} | |
} | |
private IReadOnlyCollection<AppointmentItem> GetFuturePublicHolidaysFromCalendar(DateTime startDate) | |
{ | |
var calendarItems = new List<AppointmentItem>(); | |
MAPIFolder calendarFolder = outlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar); | |
foreach (var item in calendarFolder.Items) | |
{ | |
var calendarItem = item as AppointmentItem; | |
if (calendarItem != null && calendarItem.BusyStatus == OlBusyStatus.olOutOfOffice && calendarItem.Categories.Contains("Public Holidays") && calendarItem.Start.Date >= startDate.Date) | |
{ | |
calendarItems.Add(calendarItem); | |
} | |
} | |
return calendarItems; | |
} | |
public void AddHolidayToCalendar(BankHolidayEvent bankHolidayEvent) | |
{ | |
Console.WriteLine($"Creating '{bankHolidayEvent.Title}' on {bankHolidayEvent.Date:yyyy-MM-dd}"); | |
if(!debugMode) | |
{ | |
var newAppointment = outlook.CreateItem(OlItemType.olAppointmentItem) as AppointmentItem; | |
newAppointment.AllDayEvent = true; | |
newAppointment.BusyStatus = OlBusyStatus.olOutOfOffice; | |
newAppointment.Start = bankHolidayEvent.Date.Date; | |
newAppointment.Subject = bankHolidayEvent.Title; | |
newAppointment.Categories = "Public Holidays"; | |
newAppointment.ReminderSet = false; | |
newAppointment.Save(); | |
} | |
} | |
public void UpdateHolidaySubject(AppointmentItem appointment, BankHolidayEvent bankHolidayEvent) | |
{ | |
Console.WriteLine($"Update event on {appointment.Start.Date:yyy-MM-dd} from subject '{appointment.Subject}' to '{bankHolidayEvent.Title}'"); | |
if(!debugMode) | |
{ | |
appointment.Subject = bankHolidayEvent.Title; | |
appointment.Save(); | |
} | |
} | |
public class BankHolidays | |
{ | |
public DivisionHolidays EnglandAndWales { get; } | |
public BankHolidays([JsonProperty("england-and-wales")]DivisionHolidays englandAndWales) | |
{ | |
EnglandAndWales = englandAndWales; | |
} | |
} | |
public class DivisionHolidays | |
{ | |
public List<BankHolidayEvent> Events { get; } | |
public DivisionHolidays(List<BankHolidayEvent> events) | |
{ | |
Events = events; | |
} | |
} | |
public class BankHolidayEvent | |
{ | |
public string Title { get; } | |
public DateTime Date { get; } | |
public BankHolidayEvent(string title, DateTime date) | |
{ | |
Title = title; | |
Date = date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment