我让自己在这里变得非常困惑并且在圈子里走来走去。
我有这个基本代码:
public async Task<bool> CreateEventData(OutlookCalendarData.OutlookCalendarData oData)
{
try
{
await oData.CreateEvent(oData.Weeks[0], _graphClient);
}
catch (Exception ex)
{
SimpleLog.Log(ex);
}
return true;
}
代码正在调用:
public async Task<bool> CreateEvent(Week oWeek, GraphServiceClient graphClient)
{
// Event Body
// TODO: Build proper body text
ItemBody body = new ItemBody
{
Content = oWeek.WeeklyBibleReading,
ContentType = BodyType.Text
};
// Start Time
string strStartDateTime = oWeek.Date.ToString("yyyy-MM-dd") + "T" + oWeek.StartTime + ":00";
DateTime dateStartDateTime = DateTime.Parse(strStartDateTime);
DateTimeTimeZone startTime = new DateTimeTimeZone
{
DateTime = dateStartDateTime.ToString("o"),
TimeZone = TimeZoneInfo.Local.Id
};
// End Time
DateTimeTimeZone endTime = new DateTimeTimeZone
{
DateTime = dateStartDateTime.AddMinutes(oWeek.EventDuration).ToString("o"),
TimeZone = TimeZoneInfo.Local.Id
};
// Add the event
Event createdEvent = await graphClient.Me.Calendars[_CalendarID]
.Events
.Request()
.AddAsync(new Event
{
Subject = oWeek.Title,
Body = body,
Start = startTime,
End = endTime,
IsReminderOn = true,
ReminderMinutesBeforeStart = 1024,
});
// Extended Properties
if (createdEvent != null)
{
createdEvent.SingleValueExtendedProperties.Add(
new SingleValueLegacyExtendedProperty
{
Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
Value = "CLM_MidweekMeeting"
});
return true;
}
return false;
}
但是我在这一点上得到了例外:
Event createdEvent = await graphClient.Me.Calendars[_CalendarID]
.Events
.Request()
.AddAsync(new Event
{
Subject = oWeek.Title,
Body = body,
Start = startTime,
End = endTime,
IsReminderOn = true,
ReminderMinutesBeforeStart = 1024,
});
例外:
<LogEntry Date="2017-09-01 18:38:08" Severity="Exception" Source="OutlookCalIFConsole.Outlook+<CreateEventData>d__17.MoveNext" ThreadId="10">
<Exception Type="System.NullReferenceException" Source="OutlookCalIFConsole.OutlookCalendarData.OutlookCalendarData+<CreateEvent>d__10.MoveNext">
<Message>Object reference not set to an instance of an object.</Message>
<StackTrace> at OutlookCalIFConsole.OutlookCalendarData.OutlookCalendarData.<CreateEvent>d__10.MoveNext() in D:\My Programs\2017\OutlookCalIFConsole\OutlookCalIFConsole\OutlookCalendarData\OutlookCalendarData.cs:line 97
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at OutlookCalIFConsole.Outlook.<CreateEventData>d__17.MoveNext() in D:\My Programs\2017\OutlookCalIFConsole\OutlookCalIFConsole\Outlook.cs:line 186</StackTrace>
</Exception>
</LogEntry>
我很困惑。我已经验证我使用的所有位都不是空的。
我将代码移动了一下,现在可以确认它确实在日历中创建了事件。例外情况是扩展属性:
// Extended Properties
if (createdEvent != null)
{
createdEvent.SingleValueExtendedProperties.Add(
new SingleValueLegacyExtendedProperty
{
Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
Value = "CLM_MidweekMeeting"
});
return true;
}
啊,在调试模式下,经过仔细检查,SingleValueExtendedProperties
对象中的Event
为空。我无法弄清楚如何构建一组新的属性。
我试过这个,但它不喜欢它:
List<SingleValueLegacyExtendedProperty> extendedProperties =
new List<SingleValueLegacyExtendedProperty>();
extendedProperties.Add(new SingleValueLegacyExtendedProperty
{
Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
Value = "CLM_MidweekMeeting"
});
// Add the event
Event createdEvent = await _graphClient.Me.Calendars[oData.CalendarID]
.Events
.Request()
.AddAsync(new Event
{
Subject = oWeek.Title,
Body = body,
Start = startTime,
End = endTime,
IsReminderOn = true,
ReminderMinutesBeforeStart = 1024,
SingleValueExtendedProperties = extendedProperties
});
我读过这个:
然而我无法理解为什么我的方法真的失败了。
答案 0 :(得分:1)
您走在正确的轨道上,您需要初始化extendedProperties
属性。然而,它不是您想要的List<T>
,而是EventSingleValueExtendedPropertiesCollectionPage
。
请改为尝试:
var extendedProperties = new EventSingleValueExtendedPropertiesCollectionPage();
extendedProperties.Add(new SingleValueLegacyExtendedProperty
{
Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
Value = "CLM_MidweekMeeting"
});