Search code examples
c#google-apigoogle-calendar-apigoogle-api-dotnet-client

Insert event to specific calendar in Google Calendar C#


I have written an application that will add an event to Google Calendar. This works fine but is there a way that I can add this event to a specific calendar rather than the primary?

This is my code:

        var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        new ClientSecrets
                        {
                            ClientId = "#",
                            ClientSecret = "#",
                        },
                        new[] { CalendarService.Scope.Calendar },
                        "user",
                        CancellationToken.None).Result;

        // Create the service.
        var service = new CalendarService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
            ApplicationName = "Custom CRM",
        });
        var myEvent = new Event
        {
            Summary = "Test contact",
            Location = "1 Longford Drive",
            ColorId = "6",
            
            Start = new EventDateTime()
            {
                DateTime = new DateTime(2022, 2, 27, 14, 0, 0),
                TimeZone = "Asia/Karachi"
            },
            End = new EventDateTime()
            {
                DateTime = new DateTime(2022, 2, 28, 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 = "youemail@yahoo.com"}
            //},
        };

        var recurringEvent = service.Events.Insert(myEvent, "primary");
        recurringEvent.SendNotifications = true;
        recurringEvent.Execute();

        MessageBox.Show("Event added!");

And I want to add the event to the calendar "test":

enter image description here


Solution

  • Yes you can supply a calendar id of any calendar that the user has access to.

    All users have a primary calendar. By saying primary you are just adding it there.

    var recurringEvent = service.Events.Insert(myEvent, "primary");

    If you know the calendar id then you can just supply the calendar id in stead of the word primary.

    You can use the CalendarList.List to get a list of all of the users calendars in their calendar list. This is the list at the bottom left of the Google calendar web app.