Google Calendar API v3 with C# - Unable to insert recurrence exceptions.

203 views
Skip to first unread message

Steve Barker

unread,
Mar 2, 2012, 9:37:22 AM3/2/12
to Google Calendar API
Hi all,

This is a continuation of a previous post:

http://groups.google.com/group/google-calendar-api/browse_thread/thread/4ba219a01d705d14/876a45ba16ebb55d

So, now I can save recurring appointments, I want to add recurrence
exceptions. I have the following code:

======================

const string GoogleDateFormatString = "yyyy-MM-ddTHH:mm:
00Z";

IAuthenticator authenticator = new
OAuth2LeggedAuthenticator("XXX", "XXX", "XXX", "XXX");
var service = new CalendarService(authenticator);
service.Key = "XXX";

var list = service.Events.List("primary");
var fetch = list.Fetch();

var start = new EventDateTime();
start.DateTime = "2012-03-05T10:30:00Z";
start.TimeZone = "+0000";

var end = new EventDateTime();
end.DateTime = "2012-03-05T10:35:00Z";
end.TimeZone = "+0000";

var attendee = new EventAttendee();
attendee.Email = "XXX";

var e = new Event();

e.End = end;
e.Kind = "calendar#event";
e.Organizer = new Event.OrganizerData { Email = "XXX" };
e.Start = start;
e.Summary = "Summary";
e.Id = null;
e.ICalUID = null;
e.Location = "";
e.Attendees = new List<EventAttendee>();
e.Attendees.Add(attendee);

var eventReminder = new EventReminder();
eventReminder.Method = "popup";
eventReminder.Minutes = 15;

e.Reminders = new Event.RemindersData();
e.Reminders.UseDefault = false;
e.Reminders.Overrides = new List<EventReminder>();
e.Reminders.Overrides.Add(eventReminder);

// Recurrence:
var recurrence =
"RRULE:FREQ=DAILY;COUNT=20;INTERVAL=1;WKST=SU";

e.Recurrence = new List<string>();
e.Recurrence.Add(recurrence);
e.RecurringEventId = "12345";
e.OriginalStartTime = start;

var request = service.Events.Insert(e, "primary");
request.SendNotifications = false;
request.Fetch();

// Recurrence exception:
var instances = service.Events.Instances("primary",
e.RecurringEventId).Fetch();

var instancetoChange =
instances.Items.FirstOrDefault(x => x.Start.DateTime == new
DateTime(2012, 3, 7, 10, 30, 0).ToString(GoogleDateFormatString));
instancetoChange.Start.DateTime = new DateTime(2012,
3, 7, 12, 0 ,0).ToString(GoogleDateFormatString);
instancetoChange.Start.DateTime = new DateTime(2012,
3, 7, 12, 30 ,0).ToString(GoogleDateFormatString);

var updateRequest =
service.Events.Update(instancetoChange, "primary",
instancetoChange.Id);
updateRequest.SendNotifications = false;
updateRequest.Fetch();


==================

I'm getting a null error in relation to instances.Items.

Any thoughts?

Thanks in advance,

Steve.

Steve Barker

unread,
Mar 2, 2012, 9:55:20 AM3/2/12
to Google Calendar API
I've managed to solve this too. The end of the code should be as
follows:

========================

var request = service.Events.Insert(e, "primary");
request.SendNotifications = false;
var parent = request.Fetch();

// Recurrence exception:
var instances = service.Events.Instances("primary",
parent.Id).Fetch();

var instancetoChange =
instances.Items.FirstOrDefault(x => x.Start.DateTime == new
DateTime(2012, 3, 7, 10, 30, 0).ToString(GoogleDateFormatString));
instancetoChange.Start.DateTime = new DateTime(2012,
3, 7, 12, 0 ,0).ToString(GoogleDateFormatString);
instancetoChange.End.DateTime = new DateTime(2012, 3,
7, 12, 30 ,0).ToString(GoogleDateFormatString);

var updateRequest =
service.Events.Update(instancetoChange, "primary",
instancetoChange.Id);
updateRequest.SendNotifications = false;
updateRequest.Fetch();


======================

I hope this helps someone else. I've wasted a lot of time wrestling
with this, and being disappointed with the standard of the
documentation.

Steve.


On Mar 2, 2:37 pm, Steve Barker <steve_barker...@hotmail.com> wrote:
> Hi all,
>
> This is a continuation of a previous post:
>
> http://groups.google.com/group/google-calendar-api/browse_thread/thre...
Reply all
Reply to author
Forward
0 new messages