2011-05-20 94 views
0

我試圖編輯一個工具以允許用戶從其日曆列表中進行選擇,然後清除所有事件條目/添加基於Microsoft的新條目項目任務。 繼承人原始工具:http://daball.github.com/Microsoft-Project-to-Google-Calendar/如何從Google日曆API中刪除特定日曆中的所有事件條目.NET

我對Google API /日曆API完全沒有經驗,並且遇到了一些麻煩。我正在編輯的程序會跟蹤用戶從日曆列表中選擇的哪個CalendarEntry。我目前試圖做的是創建一個EventFeed,它給我所選日曆的EventEntries,所以我可以刪除所有日曆。這樣做的目的是允許某人在更改時使用此工具更新項目文件中的日曆。這是我的函數試圖刪除。

private void clearPreviousCalendarEntries(CalendarEntry calendarEntry) 
    {  
     EventQuery query = new EventQuery(); 
     query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri); 

     EventFeed feed = (EventFeed)calendarService.Query(query); 

     AtomFeed batchFeed = new AtomFeed(feed); 

     foreach (EventEntry entry in feed.Entries) 
     { 
      entry.Id = new AtomId(entry.EditUri.ToString()); 
      entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete); 
      batchFeed.Entries.Add(entry); 
     } 

     EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(feed.Batch)); 

     foreach (EventEntry entry in batchResultFeed.Entries) 
     { 
      if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201) 
       this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Problem deleteing " + entry.Title.Text + " error code: " + entry.BatchData.Status.Code); 
      else 
       this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Deleted " + entry.Title.Text); 
     } 

    } 

我的提要沒有返回我希望的結果,但說實話我不確定如何正確地請求這些事件。

query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri);是我從中添加事件到特定的日曆

AtomEntry insertedEntry = calendarService.Insert(new Uri(calendarEntry.Links[0].AbsoluteUri), eventEntry);

這些職位肯定涉及到什麼我正在尋找的程序的一部分抓住了,但我還沒到在溶液

回答

0

嘗試是這樣的:

  CalendarService myService = new CalendarService("your calendar name"); 
      myService.setUserCredentials(username, password); 

      CalendarEntry calendar; 

      try 
      { 
       calendar = (CalendarEntry)myService.Get(http://www.google.com/calendar/feeds/default/owncalendars/full/45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com); 
       foreach (AtomEntry item in calendar.Feed.Entries) 
       { 
        item.Delete(); 
       } 
      } 
      catch (GDataRequestException) 
      { 
      } 

您可以找到 「日曆ID」(是這樣的:45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com)從日曆詳細信息頁谷歌日曆中。

這是一個相關的帖子:

google calendar api asp.net c# delete event

這是一個有用的文檔:

http://code.google.com/intl/it-IT/apis/calendar/data/2.0/developers_guide_dotnet.html

+0

在你的例子中,calendar.Feed.Entries不給EventEntries,它給了CalendarEntries。 – 2011-05-23 20:57:24

+0

我也許應該直接說出它,但我正在尋找一種解決方案,我可以根據已有的CalendarEntry查詢EventEntries,而不是使用從Google日曆內收集的特定ID – 2011-05-23 20:59:18

0

一種方法,我終於到達了從日曆中的URI收集calendarID ,然後使用該ID創建一個新的EventQuery。下面的代碼的新版本以上

private void clearPreviousCalendarEntries(CalendarEntry calendarEntry) 
    { 
     this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Clearing previous calender entries"); 

     String calendarURI = calendarEntry.Id.Uri.ToString(); 
     //The last part of the calendarURI contains the calendarID we're looking for 
     String calendarID = calendarURI.Substring(calendarURI.LastIndexOf("/")+1); 

     EventQuery query = new EventQuery(); 
     query.Uri = new Uri("http://www.google.com/calendar/feeds/" + calendarID + "/private/full"); 

     EventFeed eventEntries = calendarService.Query(query); 

     AtomFeed batchFeed = new AtomFeed(eventEntries); 

     foreach (AtomEntry entry in eventEntries.Entries) 
     { 
      entry.Id = new AtomId(entry.EditUri.ToString()); 
      entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete); 

      batchFeed.Entries.Add(entry); 
     } 

     EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(eventEntries.Batch)); 

     //check the return values of the batch operations to make sure they all worked. 
     //the insert operation should return a 201 and the rest should return 200 
     bool success = true; 
     foreach (EventEntry entry in batchResultFeed.Entries) 
     { 
      if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201) 
      { 
       success = false; 
       listBoxResults.SelectedIndex = listBoxResults.Items.Add("The batch operation for " + 
        entry.Title.Text + " failed."); 
      } 
     } 

     if (success) 
     { 
      listBoxResults.SelectedIndex = listBoxResults.Items.Add("Calendar event clearing successful!"); 
     } 

    } 

我不是這個特別的快樂,它似乎很奇怪使用字符串操作來收集信息和一起砍我自己的查詢。但它有效,我一直在努力尋找一種方法來完成這件事。

相關問題