Creating recurring events via Google Calendar API v3 - c#

I've got a web application that a user fills out a form and on submit it adds an event to a google calendar. I can insert a single instance of an event onto the calendar but when I try to set the recurrence of the event I am unable to do so. My methods so far have either accomplished nothing or the event is created, but only for a single occurrence.
I am using google api calendar v3 below is my code to insert the event.
try{
Google.Apis.Calendar.v3.CalendarService g = new
Google.Apis.Calendar.v3.CalendarService();
Google.Apis.Calendar.v3.Data.Event ev = new
Google.Apis.Calendar.v3.Data.Event();
//Create Date Times for start and end time
EventDateTime starter = new EventDateTime();
starter.DateTime = start;
EventDateTime ender = new EventDateTime();
ender.DateTime = end;
//Add values to the event
ev.Start = starter;
ev.End = ender;
ev.Summary = summary.Text;
ev.Location = location.Text;
ev.Description = description.Text;
String[] recd = {"RRULE:FREQ=WEEKLY;COUNT=2"};
Random rnd = new Random();
ev.RecurringEventId = "asdf" + rnd.Next(9999).ToString();
ev.Recurrence = recd;
//Add to calendar
addEvent(service, ev);
g.Events.Insert(ev, "********");
}
EDIT 1:
I've rewritten my event creation code as follows:
EventDateTime starter = new EventDateTime();
starter.DateTime = start;
EventDateTime ender = new EventDateTime();
ender.DateTime = end;
Event newEvent = new Event()
{
Summary = summary.Text,
Location = location.Text,
Description = description.Text,
Start = starter,
End = ender,
Recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"}
};
String calendarId = "****#group.calendar.google.com";
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();
The only problem being that the event will not be created nor its recurrence. If I leave off that line, the code will run and insert the single event into the calendar.

Did you forget to call .execute() ?
Event newEvent = new Event()
{
Summary = "Read Awesome Blog posts by Linda ",
Location = "1600 Amphitheatre Parkway., Mountain View, CA 94043",
Description = "A chance to learn more about Google APIs.",
Start = new EventDateTime()
{
DateTime = DateTime.Parse("2015-09-20T09:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
End = new EventDateTime()
{
DateTime = DateTime.Parse("2015-09-20T17:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "test#test.com" },
},
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 24 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();

Related

Get event ID of Google Calendar event after it has been created C#

I've written some code to add an event to Google Calander:
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "ID",
ClientSecret = "Secret",
},
new[] { CalendarService.Scope.Calendar },
Account.Text,
CancellationToken.None).Result;
var service = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Custom CRM",
});
var myEvent = new Event
{
Summary = Title.Text,
Location = AppointmentLocation.Text,
ColorId = Convert.ToString(AppointmentColourInt),
Description = Description.Text,
Start = new EventDateTime()
{
DateTime = new DateTime(StartDate.Value.Year, StartDate.Value.Month, StartDate.Value.Day, Convert.ToInt32(StartTime.Text.Substring(0, 2)), Convert.ToInt32(StartTime.Text.Substring(StartTime.Text.Length - 2)), 00),
TimeZone = "Europe/London"
},
End = new EventDateTime()
{
DateTime = new DateTime(EndDate.Value.Year, EndDate.Value.Month, EndDate.Value.Day, Convert.ToInt32(EndTime.Text.Substring(0, 2)), Convert.ToInt32(EndTime.Text.Substring(EndTime.Text.Length - 2)), 00),
TimeZone = "Europe/London"
},
};
var recurringEvent = service.Events.Insert(myEvent, CalendarID);
recurringEvent.SendNotifications = true;
recurringEvent.Execute();
Is there a way I could get the ID of the event once it has been created so that I can save it to my database for future reference?
Thanks.
var recurringEvent = service.Events.Insert(myEvent, CalendarID);
recurringEvent.SendNotifications = true;
var results = recurringEvent.Execute();
Execute will return to you the object it inserted. The Id should be in there.

c# using Google.Apis.Calendar.v3 create meeting for next month every weekday 30 min event

I want to create daily weekdays event for 30 minit. it seems there is something wrong in
Recurrence = new String[] { "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;UNTIL=" + nextmonthlastdayString + "T040000Z" },
I am not able to find solution tried many things.
public async Task<ActionResult> NewEvent()
{
var credential = await GetCredentialForApiAsync();
var initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "ASP.NET MVC5 Calendar Sample",
};
var service = new CalendarService(initializer);
var today = DateTime.Now;
var nextmonth = today.AddMonths(1).AddDays(-today.Day + 1);
var nextmonthString = nextmonth.ToString("yyyyMMdd");
var nxtmonthLastDate= DateTime.DaysInMonth(nextmonth.Year, nextmonth.Month);
DateTime lastDayNextMonth = nextmonth.AddDays(-nextmonth.Day + nxtmonthLastDate);
var nextmonthlastdayString = lastDayNextMonth.ToString("yyyyMMdd");
var lastDayofMonthString = DateTime.DaysInMonth(nextmonth.Year, nextmonth.Month).ToString(nextmonth.Year + "" + nextmonth.Month + "" + nextmonth.Day);
DateTime start = DateTime.Now;
DateTime end = start + TimeSpan.FromMinutes(30);
//start = DateTime.SpecifyKind(start, DateTimeKind.Local);
//end = DateTime.SpecifyKind(end, DateTimeKind.Local);
Event newEvent = new Event()
{
Summary = " Dealer Meeting",
Location = "1600 Amphitheatre Parkway., Mountain View, CA 94043",
Description = "A chance to learn more about Google APIs.",
Start = new EventDateTime()
{
DateTime = nextmonth,
TimeZone = "Asia/Kolkata",
},
End = new EventDateTime()
{
DateTime = lastDayNextMonth,
TimeZone = "Asia/Kolkata",
},
//Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=5" },
//Recurrence = new String[] { "RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;UNTIL="+ nextmonth + "T040000Z" },
Recurrence = new String[] { "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;UNTIL=" + nextmonthlastdayString + "T040000Z" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "test#test.com" },
},
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 24 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();
return View();
}
Your RRULE is correct, the problem is theend date
As per documentation:
end
The (exclusive) end time of the event. For a recurring event, this is the end time of the first instance.
This means that you should modify
{
DateTime = lastDayNextMonth,
TimeZone = "Asia/Kolkata",
}
to
End = new EventDateTime()
{
DateTime = start + TimeSpan.FromMinutes(30),
TimeZone = "Asia/Kolkata",
}
Sample:
Start = new EventDateTime()
{
DateTime = "2021-11-01T09:00:00",
TimeZone = "Asia/Kolkata",
}
End = new EventDateTime()
{
DateTime = "2021-11-01T09:30:00",
TimeZone = "Asia/Kolkata",
}

Add conference to event Google Calendar API

I can create an event but I can't create an event with a conference.
I tried all these types: "eventHangout" "eventNamedHangout" "hangoutsMeet" but still getting Invalid conference type value
Google.GoogleApiException: 'Google.Apis.Requests.RequestError
Invalid conference type value. [400]
Errors [
Message[Invalid conference type value.] Location[ - ] Reason[invalid] Domain[global]
]
Here's the code that creates and executes an event:
CalendarService service = GetCalendarService();
EventDateTime start = new EventDateTime();
start.DateTime = new DateTime(2021, 02, 19, 18, 47, 0);
EventDateTime end = new EventDateTime();
end.DateTime = new DateTime(2021, 02, 19, 18, 50, 0);
Event newEvent = new Event();
newEvent.Start = start;
newEvent.End = end;
newEvent.Summary = "New event";
newEvent.Description = "description";
newEvent.ConferenceData = CreateConferenceData();
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
request.ConferenceDataVersion = 1;
Event createdEvent = request.Execute();
Here's the code of CreateConferenceData() method:
ConferenceData conferenceData = new ConferenceData()
{
CreateRequest = new CreateConferenceRequest()
{
RequestId = Guid.NewGuid().ToString(),
ConferenceSolutionKey = new ConferenceSolutionKey()
{
Type = "hangoutsMeet" // Change according to your preferences
};
}
};
return conferenceData;
this is my service and worked perfect conference event
_calenarService = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = GenerateCredentials(),
ApplicationName = this._applicationName
});
var myEvent = new Event
{
Summary = "Google Calendar API Testing ",
Location = "Islamabad, Punjab, Pakistan",
Start = new EventDateTime()
{
DateTime = new DateTime(2021, 4, 5, 14, 0, 0),
TimeZone = "Asia/Karachi"
},
End = new EventDateTime()
{
DateTime = new DateTime(2021, 9, 10, 15, 0, 0),
TimeZone = "Asia/Karachi"
},
Recurrence = new String[] { "RRULE:FREQ=WEEKLY;BYDAY=MO" },
//If you want to add attendee
//Attendees = new List<EventAttendee>()
//{
// new EventAttendee { Email = "......com" },
// new EventAttendee { Email = "......." }
//},
ConferenceData = new ConferenceData
{
ConferenceSolution = new ConferenceSolution
{
Key = new ConferenceSolutionKey
{
Type = "hangoutsMeet"
}
},
CreateRequest = new CreateConferenceRequest
{
RequestId = "qwerfsdiob",
ConferenceSolutionKey = new ConferenceSolutionKey
{
Type = "hangoutsMeet"
},
},
EntryPoints = new List<EntryPoint> ()
{
new EntryPoint { EntryPointType = "video" },
}
}
};
var recurringEvent = _calenarService.Events.Insert(myEvent, "primary");
recurringEvent.SendNotifications = true;
recurringEvent.ConferenceDataVersion = 1;
Event eventData = recurringEvent.Execute();

Update Google Calendar- Insert/ Delete events

I need help to create a new event inside Google calendar. I followed this link
and its working fine but now I need to add new event and delete event(existing/new) and I couldn't able to figure out how to add events. I tried this example to insert new events in calendar:link
but when i compile it gives me an error and here is the screen shot.
I will really appreciate help.
To add new event and send invite:
Event myEvent = new Event
{
Summary = "Discussion on new project",
Location = "Mentor",
Start = new EventDateTime()
{
DateTime = new DateTime(2018, 8, 16, 10, 0, 0),
TimeZone = "America/Los_Angeles"
},
End = new EventDateTime()
{
DateTime = new DateTime(2018, 8, 16, 11, 30, 0),
TimeZone = "America/Los_Angeles"
},
Recurrence = new String[] { "RRULE:FREQ=WEEKLY;BYDAY=MO"},
Attendees = new List<EventAttendee>()
{
new EventAttendee() { Email = "abc#gmail.com" }
}
};
var recurringEvent = service.Events.Insert(myEvent, "primary");
recurringEvent.SendNotifications = true;
recurringEvent.Execute();

Getting the wrong event from google calendar api

Here is my current code:
Service = new CalendarService("googleid.apps.googleusercontent.com");
Service.setUserCredentials("calenderusername#gmail.com", "passwordforaccount");
var calendarquery = new EventQuery
{
Uri =new Uri("https://www.google.com/calendar/feeds/calenderusername#gmail.com/private/full"),
StartTime = DateTime.Now,
NumberToRetrieve = 1,
SortOrder = CalendarSortOrder.#ascending
};
var feedItem = Service.Query(calendarquery).Entries.FirstOrDefault() as EventEntry;
var times = new Google.GData.Extensions.When();
times = feedItem.Times[0];
var item = new CalendarEvent
{
Description = feedItem.Content.Content,
EventDateStart = times.StartTime,
Title = feedItem.Title.Text,
EventDateEnd = times.EndTime,
URL = feedItem.Links.FirstOrDefault().HRef.Content
};
However, i get the item listed out:
EventStartDate: {10.02.2014 11:00:00}
EventEndDate: {10.02.2014 23:30:00}
But i know there is an event today(07.02.2014). Anyone have any idea what im doing wrong?
I'm confused wether to use startDate or startTime in my calendarQuery.

Categories