Andrew Truckle
Andrew Truckle

Reputation: 19197

Null exception when creating new event for calendar with Microsoft Graph

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+&lt;CreateEventData&gt;d__17.MoveNext" ThreadId="10">
    <Exception Type="System.NullReferenceException" Source="OutlookCalIFConsole.OutlookCalendarData.OutlookCalendarData+&lt;CreateEvent&gt;d__10.MoveNext">
        <Message>Object reference not set to an instance of an object.</Message>
        <StackTrace>   at OutlookCalIFConsole.OutlookCalendarData.OutlookCalendarData.&lt;CreateEvent&gt;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.&lt;CreateEventData&gt;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.

Update

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;
}

Update

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.

Update

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
    });

Update

I have read this:

https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/singlevaluelegacyextendedproperty_post_singlevalueextendedproperties

Yet I can't see why my approach fails really.

Upvotes: 1

Views: 1144

Answers (1)

Marc LaFleur
Marc LaFleur

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

Related Questions