如何实现PatternedRecurrence以生成Office 365 REST APIv2日历活事件

时间:2016-01-19 20:16:04

标签: c# calendar outlook ms-office office365

我试图制作PatternedRecurrence,以便用户可以选择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

我已经抬起了referances但这对他们来说并不是很有帮助,虽然我认为我所写的内容对工作有足够的意义,除了所有这些,如果陈述,我无法'这是一个更好的方法从System.DayOfWeek转换为Microsoft.Office365.OutlookServices.DayOfWeek

1 个答案:

答案 0 :(得分:0)

您是否有代码来捕获execption?请求的响应是什么?我们可以使用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);