Insert event to google calendar using asp.net
I have to access google calendar api with asp.net mvc application and fetch all the events from the google calendar show all in a schedular. When I started work it seems very complex as I was working google api first time. I searched on the google but didn’t find justified code sample.
I spend my weekend to implement this. I faced many problems while implementation as it’s not straightforward and spend even many hours. So at last after a lot of work hard I able to implement the google calendar api with asp.net successfully. I used DHTMLX Scheduler to add/edit google events.
I able to fetch all the events from the google calendar and bind with scheduler and also insert, update, delete the events from the google calendar using scheduler. I am going to share my working code to insert new events using asp.net mvc.
Steps to insert new event to google calendar using asp.net mvc
1. Before start you should have CLIENT ID and CLIENT SECRET, you can get it from that tutorial Read Me
2. Once you have client id and client secret key you have to create a new asp.net project using c#.
3. Please add the reference of google calendar api using nuget. Please search Google.Apis.Calendar.v3 Client Library. It will show you api and you have to install.
4. Then please create a method which will authorize first and then add event into you primary google calendar and also send invite email to assignee.
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "CLIENT ID", ClientSecret = "CLIENT SECRET", }, new[] { CalendarService.Scope.Calendar }, "user", CancellationToken.None).Result; // Create the service. var service = new CalendarService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "Calendar API Sample", }); var myEvent = new Event { Summary = "Google Calendar Api Sample Code by Mukesh Salaria", Location = "Gurdaspur, Punjab, India", Start = new EventDateTime { DateTime = new DateTime(2015, 3, 2, 6, 0, 0), }, End = new EventDateTime { DateTime = new DateTime(2015, 3, 2, 7, 30, 0), }, Recurrence = new String[] { "RRULE:FREQ=WEEKLY;BYDAY=MO" }, Attendees = new List{ new EventAttendee { Email = "programmer.mukesh01@gmail.com"} }, }; var recurringEvent = service.Events.Insert(myEvent, "primary"); recurringEvent.SendNotifications = true; recurringEvent.Execute();
You have to just replace the Client Id and Secret Key, rest work code will do. It will add event in your primary calendar and send event invite to attendees.
Happy Coding..
Hi Mukesh ,
How to retrive user google calendar into our application using asp.net and C#
Throwing ans error as redirect URI mismatch.
Everytime redirect URI url changing automatically.it is not showing what actually i created in google console developer.
You have to put the latest redirect URI, if you are running at local. Once you will deploy to server it won’t change.
There are syntax errors in the part where you create “Attendees” ;
try this :
Attendees = new List()
What error do you have, Please let me know?
It’s working in local but when I hosted its doesn’t work
I am not getting invitaion by this code.
Hi Mukesh,
am getting issue while executing the above piece of code at this line
Attendees = new List()
can you please help me to overcome this issue
What error or issue you are getting?
Here is the full code, Please checkout this.
https://github.com/mukeshsalaria01/SampleGoogleCalendarEvents
Thanks Mukesh Finally its working
Hi Mukesh,
After hosting this application into IIS its not working as expected
Please help on this
Hi Mukesh ,
I am getting issues after hosting application into IIS server, Its not working as expected .
Please help on this