如何实现模式化重复周期以创建 Office 365 REST API v2 日历事件
本文关键字:Office 创建 REST API 事件 日历 v2 周期 何实现 实现 模式化 | 更新日期: 2023-09-27 18:30:59
我试图使模式递归,以便用户可以从3个选项中进行选择,每周,每两周和每月。但是,我尝试过的代码似乎不起作用,它发布到 API 时没有错误,但没有出现在测试日历上的任何地方,而作为非模式的代码确实出现在日历上。这是我尝试设置每周模式的代码。
NewEvent.Recurrence = new PatternedRecurrence();
NewEvent.Recurrence.Range = new RecurrenceRange();
NewEvent.Recurrence.Pattern = new RecurrencePattern();
NewEvent.Recurrence.Pattern.DaysOfWeek = new List<Microsoft.Office365.OutlookServices.DayOfWeek>();
NewEvent.Recurrence.Range.Type = RecurrenceRangeType.EndDate;
NewEvent.Recurrence.Range.EndDate = start.AddYears(2).ToString();
NewEvent.Recurrence.Range.StartDate = start.ToString();
NewEvent.Recurrence.Pattern.Interval = 1;
NewEvent.Recurrence.Pattern.Type = RecurrencePatternType.Weekly;
NewEvent.Recurrence.Pattern.FirstDayOfWeek = Microsoft.Office365.OutlookServices.DayOfWeek.Monday;
if (StartDay.ToString() == "Monday")
{NewEvent.Recurrence.Pattern.DaysOfWeek.Add(Microsoft.Office365.OutlookServices.DayOfWeek.DayOfWeek.Monday); }
else if (StartDay.ToString() == "Tuesday")
{ NewEvent.Recurrence.Pattern.DaysOfWeek.Add(Microsoft.Office365.OutlookServices.DayOfWeek.DayOfWeek.Tuesday); }
... for the rest of the days of the week
我已经查找了对此的引用,但它们不是很有帮助,尽管我认为我写的内容足够有意义,除了所有这些 if 语句之外,我找不到更好的方法从 System.DayOfWeek 转换为 Microsoft.Office365.OutlookServices.DayOfWeek
你有代码来捕捉执行吗?请求的响应是什么?我们可以使用 Fiddler 来跟踪响应,如果重复约会创建成功,响应将包含有关重复的详细信息。
例如,下面是一个成功的响应:
{"@odata.context":"https://outlook.office.com/api/v2.0/$metadata#Me/Events/$entity","@odata.id":"https://outlook.office.com/api/v2.0/Users('7f4f5db6-539f-45d2-b133-26a25318269a@60c1366c-1b8f-4fcd-a190-058bfd47bcb4')/Events('AQMkADVkMTY3YmNiLTJiMDctNGU5Yi05MmM4LTFjODZkNDgxMzhkMQBGAAAE19y4nS_cT5eu67AiEA77BwB6jcCHf_RcRZqcWLJUEog7AAACAQ0AAAB6jcCHf_RcRZqcWLJUEog7AAAAA_dGpgAAAA==')","@odata.etag":"W/'"eo3Ah3/kXEWanFiyVBKIOwAAA+SaVA=='"","Id":"AQMkADVkMTY3YmNiLTJiMDctNGU5Yi05MmM4LTFjODZkNDgxMzhkMQBGAAAE19y4nS_cT5eu67AiEA77BwB6jcCHf_RcRZqcWLJUEog7AAACAQ0AAAB6jcCHf_RcRZqcWLJUEog7AAAAA_dGpgAAAA==","CreatedDateTime":"2016-01-21T23:45:06.8317353-08:00","LastModifiedDateTime":"2016-01-21T23:45:07.0973601-08:00","ChangeKey":"eo3Ah3/kXEWanFiyVBKIOwAAA+SaVA==","Categories":[],"OriginalStartTimeZone":"Pacific Standard Time","OriginalEndTimeZone":"Pacific Standard Time","ResponseStatus":{"Response":"Organizer","Time":"0001-01-01T00:00:00Z"},"iCalUId":"040000008200E00074C5B7101A82E00800000000A996F1CFE854D101000000000000000010000000D968C3A111A417438178343B150C0974","ReminderMinutesBeforeStart":15,"IsReminderOn":true,"HasAttachments":false,"Subject":"Sync up","Body":{"ContentType":"Text","Content":"Status updates, blocking issues, and next steps"},"BodyPreview":"Status updates, blocking issues, and next steps","Importance":"Normal","Sensitivity":"Normal","Start":{"DateTime":"2016-01-22T02:30:00.0000000","TimeZone":"Pacific Standard Time"},"End":{"DateTime":"2016-01-22T03:30:00.0000000","TimeZone":"Pacific Standard Time"},"Location":{"DisplayName":"Water cooler"},"IsAllDay":false,"IsCancelled":false,"IsOrganizer":true,"Recurrence":{"Pattern":{"Type":"Weekly","Interval":1,"Month":0,"DayOfMonth":0,"DaysOfWeek":["Friday"],"FirstDayOfWeek":"Sunday","Index":"First"},"Range":{"Type":"EndDate","StartDate":"2016-01-22","EndDate":"2017-01-22","RecurrenceTimeZone":"Pacific Standard Time","NumberOfOccurrences":0}},"ResponseRequested":true,"SeriesMasterId":null,"ShowAs":"Busy","Type":"SeriesMaster","Attendees":[],"Organizer":{"EmailAddress":{"Name":"Fei Xue","Address":"fx@msdnofficedev.onmicrosoft.com"}},"WebLink":"https://outlook.office365.com/owa/?ItemID=AQMkADVkMTY3YmNiLTJiMDctNGU5Yi05MmM4LTFjODZkNDgxMzhkMQBGAAAE19y4nS%2BcT5eu67AiEA77BwB6jcCHf%2BRcRZqcWLJUEog7AAACAQ0AAAB6jcCHf%2BRcRZqcWLJUEog7AAAAA%2BdGpgAAAA%3D%3D&exvsurl=1&viewmodel=ICalendarItemDetailsViewModelFactory"}
这是每周创建定期约会的代码:
OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"),
async () =>
{
// Since we have it locally from the Session, just return it here.
return token;
});
Location location = new Location
{
DisplayName = "Water cooler"
};
// Create a description for the event
ItemBody body = new ItemBody
{
Content = "Status updates, blocking issues, and next steps",
ContentType = BodyType.Text
};
// Create the event object
DateTimeTimeZone start=new DateTimeTimeZone() ;
string dateTimeFormat = "yyyy-MM-ddThh:mm:ss";
string timeZone = "Pacific Standard Time";//"Eastern Standard Time";
start.DateTime = new DateTime(2016, 1, 22, 14, 30, 0).ToString(dateTimeFormat);
start.TimeZone = timeZone;
DateTimeTimeZone end = new DateTimeTimeZone();
end.DateTime = new DateTime(2016, 1, 22, 15, 30, 0).ToString(dateTimeFormat);
end.TimeZone = timeZone;
Event newEvent = new Event
{
Subject = "Sync up",
Location = location,
Start = start,
End = end,
Body = body
};
newEvent.Recurrence = new PatternedRecurrence();
newEvent.Recurrence.Range = new RecurrenceRange();
string dateFormat = "yyyy-MM-dd";
newEvent.Recurrence.Range.EndDate = DateTime.Now.AddYears(1).ToString(dateFormat);
newEvent.Recurrence.Range.StartDate = DateTime.Now.ToString(dateFormat);
newEvent.Recurrence.Range.NumberOfOccurrences = 11;
newEvent.Recurrence.Pattern = new RecurrencePattern();
newEvent.Recurrence.Pattern.Type = RecurrencePatternType.Weekly;
newEvent.Recurrence.Pattern.Interval = 1;
newEvent.Recurrence.Pattern.DaysOfWeek= new List<Microsoft.Office365.OutlookServices.DayOfWeek>() { Microsoft.Office365.OutlookServices.DayOfWeek.Friday };
// Add the event to the default calendar
await client.Me.Events.AddEventAsync(newEvent);