V3 SOLUTION: Create, Update, Delete Event in ASP.NET / C# using Extended Property

6,945 views
Skip to first unread message

Hewbhurt Gabon

unread,
Dec 14, 2014, 9:00:10 AM12/14/14
to google-ca...@googlegroups.com
I am sharing this because I don't want others to suffer. A working .NET code for Google Calendar API V3 is no where to be found. I hope this would be of help to everybody. Thank you ms. Lucia for the ideas. This is inspired from this post at Code Project: http://www.codeproject.com/Articles/565032/Google-Calendar-Integration-in-ASP-NET-Create-ed


using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Util.Store;

        CalendarService calService;
        private const string calID = "xxxxxxxxx...@group.calendar.google.com";
       
private const string UserId = "user-id";
       
private static string gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");


My revision of Tasks.ASP.NET.SimpleOAuth2     
        public void Authenticate()
       
{
           
CalendarService service = null;

           
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
               
new GoogleAuthorizationCodeFlow.Initializer
               
{
                   
ClientSecrets = GetClientConfiguration().Secrets,
                   
DataStore = new FileDataStore(gFolder),
                   
Scopes = new[] { CalendarService.Scope.Calendar }
               
});

           
var uri = Request.Url.ToString();
           
var code = Request["code"];
           
if (code != null)
           
{
               
var token = flow.ExchangeCodeForTokenAsync(UserId, code,
                    uri
.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;

               
// Extract the right state.
               
var oauthState = AuthWebUtility.ExtracRedirectFromState(
                    flow
.DataStore, UserId, Request["state"]).Result;
               
Response.Redirect(oauthState);
           
}
           
else
           
{
               
var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
                   
CancellationToken.None).Result;
               
if (result.RedirectUri != null)
               
{
                   
// Redirect the user to the authorization server.
                   
Response.Redirect(result.RedirectUri);
               
}
               
else
               
{
                   
// The data store contains the user credential, so the user has been already authenticated.
                    service
= new CalendarService(new BaseClientService.Initializer
                   
{
                       
ApplicationName = "Calendar API Sample",
                       
HttpClientInitializer = result.Credential
                   
});
               
}
           
}

            calService
= service;
       
}

       
public static GoogleClientSecrets GetClientConfiguration()
       
{
           
using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
           
{
               
return GoogleClientSecrets.Load(stream);
           
}
       
}



Creating, updating, deleting Google Calendar Event. This uses Extended Property instead of the built-in ID of an 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;
       
}
   
}

   
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;
   
}



\App_Data\MyGoogleStorage\client_secrets.json
{
 
"web": {
   
"client_id": "18000002748-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
   
"client_secret": "G0hxxxxxxxxxxxxxxxxx624"
 
}
}

Lucia Fedorova

unread,
Dec 15, 2014, 6:38:42 PM12/15/14
to google-ca...@googlegroups.com
Hi Hewbhurt, thanks for sharing!
Also if you want to have the same IDs between two systems, consider just setting event IDs on inserts to whatever value you want (see the documentation here: https://developers.google.com/google-apps/calendar/v3/reference/events).


On Sunday, December 14, 2014 3:00:10 PM UTC+1, Hewbhurt Gabon wrote:
I am sharing this because I don't want others to suffer. A working .NET code for Google Calendar API V3 is no where to be found. I hope this would be of help to everybody. Thank you ms. Lucia for the ideas. This is inspired from this post at Code Project: http://www.codeproject.com/Articles/565032/Google-Calendar-Integration-in-ASP-NET-Create-ed


using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Util.Store;

        CalendarService calService;
        private const string calID = "xxxxxxxxxxxxxxxxxxx@group.calendar.google.com";
       
private const string UserId = "user-id";
...

Wafa Hamdi

unread,
Dec 26, 2014, 6:38:01 AM12/26/14
to google-ca...@googlegroups.com
Hi,

You can tell me how you using dll Google.Apis.Services Thanks ?
...

Hewbhurt Gabon

unread,
Dec 28, 2014, 2:47:35 AM12/28/14
to google-ca...@googlegroups.com
Google.Apis.Calendar.v3 Client Library
https://www.nuget.org/packages/Google.Apis.Calendar.v3/

Hemant Sudarshan

unread,
Feb 2, 2015, 1:00:42 AM2/2/15
to google-ca...@googlegroups.com


On Sunday, 14 December 2014 19:30:10 UTC+5:30, Hewbhurt Gabon wrote:


Creating, updating, deleting Google Calendar Event. This uses Extended Property instead of the built-in ID of an 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
...Hi
Sorry for the amateurish question
But in the testcalendar.cs of this particular project,what exactly would be the method calls for the ExpKey and ExpVal for the CreateUpdateEvent and DeleteEvent
I'm not able to figure that out
Any help would be much appreciated
Thanks

Hewbhurt Gabon

unread,
Feb 2, 2015, 8:01:55 AM2/2/15
to google-ca...@googlegroups.com
Those are KeyNames and KeyValue 
or PropertyName and PropertyValue 
you wish to get from or set to Event ExtendedProperties.
Extended Property is useful for storing small string data related to the event.

Keith Tan

unread,
Apr 29, 2015, 9:27:26 PM4/29/15
to google-ca...@googlegroups.com
do u have sample code

Yo Beanz

unread,
May 13, 2015, 12:26:28 AM5/13/15
to google-ca...@googlegroups.com
what is the outcome of compiling this code?
...

Angie Knowles

unread,
Jun 2, 2015, 1:15:52 AM6/2/15
to google-ca...@googlegroups.com
Visual Studion does not like the Request and Response objects. Which namespace do I need to include for them the compile?

Thanks!
...

mohan teja

unread,
Jun 29, 2015, 6:07:20 AM6/29/15
to google-ca...@googlegroups.com
Hi , 
I am getting attached error message , when i am trying to run your code.

error description :
...
ErrorTest.png

Mastersoft Developers Limited

unread,
Jun 3, 2020, 6:45:39 AM6/3/20
to Google Calendar API
Dear friends, Greetings to you. I hope you're fine.
I've just come across this forum with a problem that I've failed to solve for days!!! So, I'm expecting someone to help me. 

According to the posted example, I can add several Shared Extended Property to an event. But how can I retrieve these extended properties and see them on my Google Calendar or in a DataGridView. I've tried to add some extended properties to several events but I can't see them on my Google calendar!!!! And I can't figure out how to load them into the DataGridView.

Please, help me out.

Thanks
Reply all
Reply to author
Forward
0 new messages