Original article: operate Google Calendar with C # code
Topic
Use the Google. Net APIs client library and the C # code to create meeting invitations in the Google Calendar.
Background
Recently, Google released the. NET APIs client library, which can finally conveniently call Google APIs through. Net/C # code.
We have this requirement, so we tried it. The requirement is as follows: by calling the Google Calendar API, You can automatically create a Google Calendar event for the weekly meeting, invite and notify attendees, and the invitee can modify the calendar item. Previously, this operation was manually performed on the Google Calendar web page.
Preparations
1. download and install the Google data api sdk, which is mainly used to reference three of the three sets. This SDK also contains the source code of Google. Net APIs client library. It is precisely because of the source code that we encounter in use that we can quickly solve the problem.
2. Read the reference documents and sample code data API developer's Guide:. net. Focus on creating single-occurrence events (our task is to create a calendar item ). The sample code only creates a resume calendar item containing the title, content, location, and time. Our requirements also include: a) Invite participants (participant); B) notification to participants (notifications); c) the invitee can modify the calendar item (guestscanmodify ).
Problems
Google. net APIs client library does not implement the Gcal: guestscanmodify attribute (setting this attribute allows the invitee to modify the calendar item). We will refer to it later. the source code of net APIs client library is implemented by itself. The Code is as follows:
public class GuestsCanModify : EnumConstruct
{
public GuestsCanModify()
: base("guestsCanModify",
GDataParserNameTable.gCalPrefix,
GDataParserNameTable.NSGCal)
{
}
public GuestsCanModify(string value)
: base("guestsCanModify",
GDataParserNameTable.gCalPrefix,
GDataParserNameTable.NSGCal, value)
{
}
}
CodeImplementation
Create a new project in vs2010 and add three references: "Google. gdata. extensions. DLL "," Google. gdata. calendar. DLL "," Google. gdata. client. DLL ".
The code for creating a calendar item for meeting invitations is as follows:
Public void createcalendarevent ()
{
Calendarservice = new calendarservice ("cnblogsmeeting ");
Calendarservice. setusercredentials ("Google login username", "password ");
Evententry entry = new evententry ();
// Calendar title and content
Entry. Title. Text = "the title of the weekly meeting notification ";
Entry. content. content = "Contents notified by the weekly blog Forum ";
// Start and end time ~
When eventtime = new when (datetime. Now. Date. addhours (17 ),
Datetime. Now. Date. addhours (18 ));
Entry. Times. Add (eventtime );
// Participants to be invited
WHO = new WHO ();
Who. Email = "[email protected]";
Who. rel = "http://schemas.google.com/g/2005#event.attendee ";
Entry. Maid. Add (WHO );
// Send a notification to the invitee
Entry. Communications = true;
// The invitee can modify the calendar item.
Entry. extensionelements. Add (New guestscanmodify ("true "));
Uri posturi = new uri ("ttps: // www.google.com/calendar/feeds/default/private/full ");
Atomentry insertentry = calendarservice. insert (posturi, entry );
Assert. notnull (insertentry );
}
Code download
Googlecalendardemo.rar
Operate Google Calendar with C # code