2011-05-20 157 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!"); 
     } 

    } 

我不是这个特别的快乐,它似乎很奇怪使用字符串操作来收集信息和一起砍我自己的查询。但它有效,我一直在努力寻找一种方法来完成这件事。