abdelhedi hlel
abdelhedi hlel

Reputation: 3709

Delete and update Google events [Google Calendar] using ASP.NET

How I can add in my website the update and delete events to my Google Calendar using ASP.NET C#?

Upvotes: 2

Views: 3105

Answers (1)

abielita
abielita

Reputation: 13469

You can use the methods Calendars: update to update metadata for a calendar. and Calendars: delete to delete a secondary calendar. Use Calendars.clear for clearing all events on primary calendars.

Sample HTTP requests:

Update: PUT https://www.googleapis.com/calendar/v3/calendars/calendarId
Delete: DELETE https://www.googleapis.com/calendar/v3/calendars/calendarId
Clear: POST https://www.googleapis.com/calendar/v3/calendars/calendarId/clear

Found this thread with a working .NET code for Google Calendar API V3.

Update Event:

 public string CreateUpdateEvent(string ExpKey, string ExpVal, string evTitle, string evDate)
    {
        EventsResource er = new EventsResource(calService);
        var queryEvent = er.List(calID);
        queryEvent.SharedExtendedProperty = ExpKey + "=" + ExpVal; //"EventKey=9999"
        var EventsList = queryEvent.Execute();

        Event ev = new Event();
        EventDateTime StartDate = new EventDateTime();
        StartDate.Date = evDate; //"2014-11-17";
        EventDateTime EndDate = new EventDateTime();
        EndDate.Date = evDate;

        ev.Start = StartDate;
        ev.End = EndDate;
        ev.Summary = evTitle; //"My Google Calendar V3 Event!";

        string FoundEventID = String.Empty;
        foreach(var evItem in EventsList.Items)
        {
            FoundEventID = evItem.Id;
        }

        if (String.IsNullOrEmpty(FoundEventID))
        {
            //If event does not exist, Append Extended Property and create the event
            Event.ExtendedPropertiesData exp = new Event.ExtendedPropertiesData();
            exp.Shared = new Dictionary<string, string>();
            exp.Shared.Add(ExpKey, ExpVal);
            ev.ExtendedProperties = exp;
            return er.Insert(ev, calID).Execute().Summary;
        }
        else
        {
            //If existing, Update the event
            return er.Update(ev, calID, FoundEventID).Execute().Summary;
        }
    }

Delete Event:

public bool DeleteEvent(string ExpKey, string ExpVal)
    {
        EventsResource er = new EventsResource(calService);
        var queryEvent = er.List(calID);

        queryEvent.SharedExtendedProperty = ExpKey + "=" + ExpVal; //"EventKey=9999"
        var EventsList = queryEvent.Execute();

        string FoundEventID = String.Empty;
        foreach (Event ev in EventsList.Items)
        {
            FoundEventID = ev.Id;
            er.Delete(calID, FoundEventID).Execute();
            return true;
        }

        return false;
    }

Check also this Quickstart tutorial from Google documentation.

Upvotes: 2

Related Questions