2017-09-01 32 views
1

我在這裏讓自己非常困惑,並且在圈子裏四處走動。使用Microsoft Graph爲日曆創建新事件時爲空例外

我有這樣的基本代碼:

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

更新

啊,在調試模式下,仔細觀察,SingleValueExtendedPropertiesEvent對象爲空。我無法弄清楚如何構建一組新的屬性。

更新

我都試過,但它不喜歡它:

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<T>,而是EventSingleValueExtendedPropertiesCollectionPage

試試這個:

var extendedProperties = new EventSingleValueExtendedPropertiesCollectionPage(); 
extendedProperties.Add(new SingleValueLegacyExtendedProperty 
{ 
    Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1", 
    Value = "CLM_MidweekMeeting" 
}); 
+1

啊,就這麼簡單。謝謝。我注意到在手冊中,例子是'「id」:「String {66f5a359-4659-4830-9070-00040ec6ac6e} Name Fun」'。 ** Guid **有大括號。當我查看調試時他們錯過了我的內容。所以我現在要做:'Id = String.Format(「String {{{0}}} Name {1}」,Guid.NewGuid()。ToString(),「TruckleSoft1」)' –

相關問題