Reputation: 3125
I am creating a Windows10 app, specifically for WindowsPhone.
The app needs to add an appointment to the users calendar. So far I have managed to do this but the user has to authorise each appointment.
Here is the code I have (pulled pretty much straight out of the UWP examples on GitHub):
private async void CreateTestCalendarEntry_Click(object sender, RoutedEventArgs e)
{
string errorMessage = null;
var appointment = new Windows.ApplicationModel.Appointments.Appointment();
// StartTime
var date = DateTime.Now;
var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
var startTime = new DateTimeOffset(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, timeZoneOffset);
appointment.StartTime = startTime;
// Subject
appointment.Subject = "Test Calendar Entry " + date.ToString();
if (appointment.Subject.Length > 255)
{
errorMessage = "The subject cannot be greater than 255 characters.";
}
// Location
appointment.Location = "Japan";
if (appointment.Location.Length > 32768)
{
errorMessage = "The location cannot be greater than 32,768 characters.";
}
// Details
appointment.Details = "Details";
if (appointment.Details.Length > 1073741823)
{
errorMessage = "The details cannot be greater than 1,073,741,823 characters.";
}
// Duration
// All Day
appointment.AllDay = true;
// Reminder
appointment.Reminder = TimeSpan.FromDays(1);
//Busy Status
appointment.BusyStatus = Windows.ApplicationModel.Appointments.AppointmentBusyStatus.WorkingElsewhere;
// Sensitivity
appointment.Sensitivity = Windows.ApplicationModel.Appointments.AppointmentSensitivity.Public;
var rect = new Rect(new Point(Window.Current.Bounds.Width / 2, Window.Current.Bounds.Height / 2), new Size());
String appointmentId = await Windows.ApplicationModel.Appointments.AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default);
}
Using this code, the await line opens the users calendar and displays the following, where the user can authorise or delete:
Hitting the save button, does as expected.
The problem - suppose I have 20 appointments to add to the users calendar, the user isn't going to want to authorise each one. How do I create an appointment without leaving my app and without the user needing to authorise it?
Upvotes: 3
Views: 1546
Reputation: 1839
This can be done by writing to your own custom calendar. The required lines of code are these:
async private Task CreateCalenderEntry()
{
// 1. get access to appointmentstore
var appointmentStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AppCalendarsReadWrite);
// 2. get calendar
var appCustomApptCalendar = await appointmentStore.CreateAppointmentCalendarAsync("MyCalendar");
// 3. create new Appointment
var appo = new Windows.ApplicationModel.Appointments.Appointment();
// appointment properties
appo.AllDay = true;
appo.Subject = "Mein Termin";
appo.StartTime = DateTime.Now;
// 4. add
await appCustomApptCalendar.SaveAppointmentAsync(appo);
}
I just tried it on Windows 10 Desktop and it worked. Should work on W10Mobile, too. However, it requires that the user shows the custom calendar. I think this should work for you.
I once wrote a blogpost (in German) covering this: https://blogs.msdn.microsoft.com/dmx/2014/07/24/termine-im-windows-phone-per-api-anlegen-ohne-broker/ It also explains that this even works from background. (to understand the text just use http://www.bing.com/translator if code alone is not sufficient, sorry for the inconvenience).
Upvotes: 3