使用 Microsoft Graph 为日历创建新事件时出现空异常

Posted

技术标签:

【中文标题】使用 Microsoft Graph 为日历创建新事件时出现空异常【英文标题】:Null exception when creating new event for calendar with Microsoft Graph 【发布时间】:2017-09-01 17:42:30 【问题描述】:

我在这里真的很困惑并且在转圈。

我有这个基本代码:

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+&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>

我很困惑。我已经验证我使用的所有位都不为空。

更新

我稍微移动了我的代码,现在可以确认它确实在日历中创建了事件。扩展属性例外:

// Extended Properties
if (createdEvent != null)

    createdEvent.SingleValueExtendedProperties.Add(
        new SingleValueLegacyExtendedProperty
        
            Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
                Value = "CLM_MidweekMeeting"
        );

    return true;

更新

啊,在调试模式下,仔细观察,Event 对象中的SingleValueExtendedProperties 为空。我不知道如何构建一组新的属性。

更新

我试过了,但它不喜欢它:

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

更新

我已阅读:

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

但我不明白为什么我的方法真的失败了。

【问题讨论】:

【参考方案1】:

您在正确的轨道上,您需要初始化extendedProperties 属性。但是,您想要的不是List&lt;T&gt;,而是EventSingleValueExtendedPropertiesCollectionPage

试试这个:

var extendedProperties = new EventSingleValueExtendedPropertiesCollectionPage();
extendedProperties.Add(new SingleValueLegacyExtendedProperty

    Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1",
    Value = "CLM_MidweekMeeting"
);

【讨论】:

啊,这么简单。谢谢你。我注意到手册中的示例是"id":"String 66f5a359-4659-4830-9070-00040ec6ac6e Name Fun"Guid 有花括号。当我查看调试时,它们在我的身上丢失了。所以我现在做:Id = String.Format("String 0 Name 1", Guid.NewGuid().ToString(), "TruckleSoft1")

以上是关于使用 Microsoft Graph 为日历创建新事件时出现空异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 microsoft graph API 创建事件时设置自己的事件 ID

我可以使用 Microsoft Graph API 同步日历事件吗?

使用 Microsoft Graph API 将在线会议 URL 添加到事件

使用 Microsoft Graph 过滤两个扩展属性 [重复]

MS Graph API:microsoft.graph.createLink 未创建匿名视图链接

使用 Microsoft Graph API 获取修改后的系列事件发生