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..

Posted by | View Post | View Group