Reputation: 19197
I am getting myself really confused here and going around in circles.
I have this basic code:
public async Task<bool> CreateEventData(OutlookCalendarData.OutlookCalendarData oData)
{
try
{
await oData.CreateEvent(oData.Weeks[0], _graphClient);
}
catch (Exception ex)
{
SimpleLog.Log(ex);
}
return true;
}
The code is calling is here:
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;
}
But I am getting an exception raised on this bit:
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,
});
The exception:
<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>
I am confused. I have verified that all the bits I am using are not null.
I moved my code around a little bit and can now confirm that it does create the event in the calendar. The exception is on the extended properties:
// Extended Properties
if (createdEvent != null)
{
createdEvent.SingleValueExtendedProperties.Add(
new SingleValueLegacyExtendedProperty
{
Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
Value = "CLM_MidweekMeeting"
});
return true;
}
Ah, in debug mode, on closer inspection, SingleValueExtendedProperties
in the Event
object is null. I can't work out how to build a new set of properties.
I have tried this but it does not like it:
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
});
I have read this:
Yet I can't see why my approach fails really.
Upvotes: 1
Views: 1144
Reputation: 33124
You're on the right track, you need to init the extendedProperties
property. It isn't however a List<T>
that you want, it is a EventSingleValueExtendedPropertiesCollectionPage
.
Try this instead:
var extendedProperties = new EventSingleValueExtendedPropertiesCollectionPage();
extendedProperties.Add(new SingleValueLegacyExtendedProperty
{
Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
Value = "CLM_MidweekMeeting"
});
Upvotes: 1