使用 VB.Net 日历与和 ASP.NET

分享于 

13分钟阅读

Web开发

  繁體

介绍

 calendarthumb.gif

我搜索和搜索,但是我找不到完整的例子,从很多不同的例子中,我决定发布这个小项目。

背景

有几个类似的文章,它的中一个是代码项目中的一个: 使用谷歌日历和 ASP.NET by Igor Igor Alekseev。

当我找到他的文章时,我很难让我的事件进入我想要的日历。

在以后的功能中,我已经模仿过igor对象 ByRef的方法,因为在避免被 Google Ban Hammer smacked 这在测试中可能会提供一些有趣的错误

但是最重要的是,他对这一行代码的直觉是:

calFeed.Entries(x).Id.AbsoluteUri.Substring(63) 

在我发现" substring(63)"之前,我在获得一个可以靠的日历i的helluva时间 !

感谢 Igor !

使用代码

我尝试用这个代码维护两个不同的方法,一个是一个调用,另一个是复合调用。 在我自己的应用程序中使用单个调用时,例如将新行添加到数据库的表中。 我可以打电话,把数据推到日历上。 这种方法也非常适合于Web服务调用。 当你需要在客户端代码中执行多个步骤时,它允许你在一次调用中获得 CalendarService 对象。

一次调用通常如下所示:

Dim g AsNew GoogleCalSvc, str asStringstr = g.A1ListMyCalendars

多个或者复合调用的外观可能如下所示:

Dim g AsNew GoogleCalendar_KellyTours, str asstringstr = str & g.FindEvent(g.GetCalendarService, "123")

或者,甚至像这样,服务对象 s 被重复使用:

Dim g AsNew GoogleCalSvcDim s = g.GetCalendarServiceDim out AsString = g.FindEvent(s, "Primary", "123")
out = out & g.FindEvent(s, "Primary", "456")
out = out & g.FindEvent(s, "Primary", "789")Me.output.InnerHtml = out

Points of Interest

一旦互联网上的大多数代码和文档都用于 C# 中的项目,就会发现API实际上很容易。 随着持续发展,该项目可以很容易地进入全部日历访问的完整特性包装接口。

在我自己的使用中,我还有额外的数据库代码,所以我可以在日历中自动发布。更新。等等。

代码列表

Imports System.IOImports System.Data.SqlClientImports System.Web.ServicesImports System.Web.Services.ProtocolsImports System.ComponentModel'Imports Google.GData.AccessControlImports Google.GData.CalendarImports Google.GData.ClientImports Google.GData.Extensions' To allow this Web Service to be called from script,' using ASP.NET AJAX, uncomment the following line.' <System.Web.Script.Services.ScriptService()> _<System.Web.Services.WebService(Namespace:="http://localhost/GoogleCalendarInVBNET/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _PublicClass GoogleCalSvc
 Inherits System.Web.Services.WebService
 PrivateConst userName AsString = "SomeUser@GMailOrGoogleAppsDomain.com"PrivateConst userPassword AsString = "SomeStrongPassword"'' Here's the feed to access events on the users' private/primar calendar:Private feedUri AsString = _
 "https://www.google.com/calendar/feeds/default/private/full?max-results=9999"'' Here's the feed that lists all calendars that this user has access to:PrivateConst feedOwnCalendars AsString = _
 "https://www.google.com/calendar/feeds/default/owncalendars/full"'' This is a feed for non-primary calendars that requires an ID field,'' taken from enumerating the users' calendars...PrivateConst CALENDAR_TEMPLATE AsString = _
 "https://www.google.com/calendar/feeds/{0}/private/full?max-results=9999"#Region"Single Call Public Functions" 
 <WebMethod()> _
 PublicFunction A1ListMyCalendars() AsStringDim str AsString str = ListAllCalendars()
 Return str
 EndFunction <WebMethod()> _
 PublicFunction A1CreateEvent(ByVal stCalName AsString, ByVal strWhere AsString, _
 ByVal strStart AsString, ByVal strEnd AsString, _
 ByVal strSubject AsString, ByVal strBody AsString, _
 ByVal strRecurData AsString) AsStringReturn CreateEvent(GetCalendarService, stCalName, strWhere, _
 strStart, strEnd, strSubject, strBody, strRecurData)
 EndFunction <WebMethod()> _
 PublicFunction A1DeleteEvent(ByVal intTripNo AsInteger, ByVal stCalName AsString) AsStringReturn DeleteEvent(GetCalendarService, intTripNo, stCalName)
 EndFunction <WebMethod()> _
 PublicFunction A1ListUsersCalendars() AsStringDim Service As CalendarService = GetCalendarService()
 Dim query AsNew FeedQuery
 query.Uri = New Uri(feedUri) ' Private feedUri 'cause for users calendarsDim calFeed As AtomFeed
 calFeed = Service.Query(query)
 Dim str AsString = ""For x AsInteger = 0To calFeed.Entries.Count - 1 str = str & calFeed.Entries(x).Title.Text
 If calFeed.Entries.Count> 1And x <(calFeed.Entries.Count - 1) Then str = str & "," & calFeed.Title.Text & vbCrLf
 EndIfNextReturn str
 EndFunction#End Region#Region"Public GET Functions" 
 PublicFunction GetCalendarService() As CalendarService
 Dim Service As CalendarService = New CalendarService("My-exampleApp-1")
 Service.setUserCredentials(userName, userPassword)
 Return Service
 EndFunction <WebMethod()> _
 PublicFunction GetCalendarID(ByRef Service As CalendarService, ByVal strCalName AsString) AsStringDim query AsNew FeedQuery
 Dim str AsString = "" query.Uri = New Uri(feedOwnCalendars)
 Dim calFeed As AtomFeed
 calFeed = Service.Query(query)
 For x AsInteger = 0To calFeed.Entries.Count - 1If calFeed.Entries(x).Title.Text = strCalName Then str = calFeed.Entries(x).Id.AbsoluteUri.Substring(63)
 EndIfNextReturn str
 EndFunction <WebMethod()> _
 PublicFunction GetCalendarURI(ByRef Service As CalendarService, ByVal stCalName AsString) AsStringDim stURI AsString = String.Format(CALENDAR_TEMPLATE, GetCalendarID(Service, stCalName))
 '' Easy override to go back to the users' primary calendar!!!If stCalName = "Primary"Then stURI = feedUri
 EndIfReturn stURI
 EndFunction#End Region <WebMethod()> _
 PublicFunction ListAllCalendars() AsStringDim Service As CalendarService = GetCalendarService()
 Dim query AsNew FeedQuery
 query.Uri = New Uri(feedOwnCalendars)
 Dim calFeed As AtomFeed
 calFeed = Service.Query(query)
 Dim str AsString = ""For x AsInteger = 0To calFeed.Entries.Count - 1 str = str & calFeed.Entries(x).Title.Text & "|" & _
 calFeed.Entries(x).Id.AbsoluteUri.Substring(63) & vbCrLf
 NextReturn str
 EndFunction <WebMethod()> _
 PublicFunction DeleteAllEvents(ByVal strCalName AsString) AsStringDim str AsString = ""Dim Service As CalendarService = GetCalendarService()
 Dim query AsNew FeedQuery
 query.Uri = New Uri(GetCalendarURI(Service, strCalName))
 Dim calFeed As AtomFeed
 calFeed = Service.Query(query)
 If calFeed.Entries.Count> 0ThenFor x AsInteger = 0To calFeed.Entries.Count - 1 str = str & calFeed.Entries(x).Title.Text & vbCrLf
 calFeed.Entries(x).Delete()
 NextEndIfReturn str
 EndFunctionPublicFunction DeleteEvent(ByRef Service As CalendarService, _
 ByVal strSearchFor AsString, ByVal stCalName AsString) AsStringDim str AsString = ""Dim query AsNew FeedQuery
 query.Uri = New Uri(GetCalendarURI(Service, stCalName))
 query.Query = strSearchFor
 Dim sFeed As EventFeed = Service.Query(query)
 Dim Exists AsBoolean = FalseIf sFeed.TotalResults> 0Then Exists = TrueDim calFeed As AtomFeed
 calFeed = Service.Query(query)
 If calFeed.Entries.Count> 0ThenFor x AsInteger = 0To calFeed.Entries.Count - 1 str = str & calFeed.Entries(x).Title.Text
 calFeed.Entries(x).Delete()
 NextEndIfEndIfReturn str
 EndFunctionPrivateFunction CreateEvent(ByRef Service As CalendarService, _
 ByVal strCalName AsString, _
 ByVal strWhere AsString, _
 ByVal strStart AsString, _
 ByVal strEnd AsString, _
 ByVal strSubject AsString, _
 ByVal strBody AsString, _
 ByVal strRecurData AsString) AsStringDim entry AsNew EventEntry
 entry.Title.Text = strSubject
 entry.Content.Content = strBody
 Dim eventLocation AsNew Where
 eventLocation.ValueString = strWhere
 entry.Locations.Add(eventLocation)
 Dim eventTime AsNew [When](strStart, strEnd)
 entry.Times.Add(eventTime)
 Dim postUri As Uri = New Uri(GetCalendarURI(Service, strCalName))
 Dim insertedEntry As AtomEntry = Service.Insert(postUri, entry)
 Return insertedEntry.ToString
 EndFunctionPublicFunction FindEvent(ByRef Service As CalendarService, _
 ByVal strCalName AsString, ByVal strSearchFor AsString) AsStringDim strOut AsString = ""Dim query As EventQuery = New EventQuery
 query.Uri = New Uri(GetCalendarURI(Service, strCalName))
 query.Query = strSearchFor.ToString
 Dim calFeed As EventFeed = Service.Query(query)
 Dim feedEntry As EventEntry
 If calFeed.TotalResults> 0ThenForEach feedEntry In calFeed.Entries
 strOut = strOut & (feedEntry.Title.Text) & vbCrLf
 NextElse strOut = "No Results found"EndIfReturn strOut
 EndFunction <WebMethod()> _
 PublicFunction ListUserEvents() AsStringDim strOut AsString = ""Dim query As EventQuery = New EventQuery
 Dim service As CalendarService = GetCalendarService()
 query.Uri = New Uri(GetCalendarURI(service, "Primary"))
 Dim calFeed As EventFeed = service.Query(query)
 Dim feedEntry As EventEntry
 ForEach feedEntry In calFeed.Entries
 strOut = strOut & (feedEntry.Title.Text) & vbCrLf
 NextReturn strOut
 EndFunction <WebMethod()> _
 PublicFunction ListEvents(ByVal strCalName AsString) AsStringDim strOut AsString = ""Dim query As EventQuery = New EventQuery
 Dim Service As CalendarService = GetCalendarService()
 query.Uri = New Uri(GetCalendarURI(Service, strCalName))
 Dim calFeed As EventFeed = Service.Query(query)
 Dim feedEntry As EventEntry
 ForEach feedEntry In calFeed.Entries
 strOut = strOut & (feedEntry.Title.Text) & vbCrLf
 ' list the event title in a messagebox...NextReturn strOut
 EndFunction <WebMethod()> _
 PublicFunction DeDupeEvents(ByVal strCalName AsString) AsStringDim strOut AsString = "", strVal AsString = ""Dim vals AsNew Collection
 ' list all the events....Dim query As EventQuery = New EventQuery
 Dim service As CalendarService = GetCalendarService()
 query.Uri = New Uri(GetCalendarURI(service, strCalName))
 Dim calFeed As EventFeed = service.Query(query)
 Dim feedEntry As EventEntry
 ForEach feedEntry In calFeed.Entries
 strVal = (feedEntry.Title.Text)
 IfNot vals.Contains(strVal) Then strOut = strOut & strVal
 vals.Add(strVal, strVal)
 Else feedEntry.Delete()
 EndIfNextReturn strOut
 EndFunctionEndClass

历史记录

  • 12/31/2011 - 这是我第一次发布这篇文章和代码,我相信这是我第一次从应用程序中分离出来。

asp  asp-net  CAL  Using  日历  Google日历  
相关文章