使用Microsoft Graph

时间:2017-08-28 13:46:05

标签: c# outlook microsoft-graph

我已经看过几个问题,但不能引用这个问题。我调整了我的代码以包含每个日历中的事件信息:

   public void DisplayCalendarsList(bool bDetailed)
    {
        try
        {
            foreach (Calendar oCalendar in _ListCalendars)
            {
                string strCalendarInfo = $"Calendar Name: {oCalendar.Name}";
                strCalendarInfo += $" Calendar Id: {oCalendar.Id}";
                Console.WriteLine(strCalendarInfo);

                if(bDetailed)
                {
                    if(oCalendar.Events?.Count > 0)
                    {
                        foreach (Event oEvent in oCalendar.Events)
                        {
                            string strEventInfo = $"Subject: {oEvent.Subject}" + Environment.NewLine;
                            strEventInfo += $"Body: {oEvent.Body}" + Environment.NewLine;
                            strEventInfo += $"Times: Start - {oEvent.Start} End - {oEvent.End}" + Environment.NewLine;
                            strEventInfo += $"Location: {oEvent.Location}" + Environment.NewLine;
                            strEventInfo += $"All Day: {oEvent.IsAllDay}" + Environment.NewLine;
                            Console.WriteLine(strEventInfo);
                        }

                    }
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine($"Error DisplayCalendarsList: {ex.Message}");
        }
     }

但在每种情况下,oEvent都是null而且我不知道为什么。

更新

例如,我知道 Keynsham Congregation 日历中有两个测试事件。然而:

Debug Output

Microsoft Snippets 执行此操作:

// Get events in all the current user's mail folders.
public async Task<List<ResultsItem>> GetMyEvents(GraphServiceClient graphClient)
{
    List<ResultsItem> items = new List<ResultsItem>();

    // Get events.
    IUserEventsCollectionPage events = await graphClient.Me.Events.Request().GetAsync();

    if (events?.Count > 0)
    {
        foreach (Event current in events)
        {
            items.Add(new ResultsItem
            {
                Display = current.Subject,
                Id = current.Id
            });
        }
    }
    return items;
}

但背景不一样。我没有使用graphClient.Me.Events,因为它不是我想要的。

更新2

我找到了另一个帮助我的资源。我调整了这样的事情,以便不使用oCalendar.Events而是使用图表:

public async Task BuildCalendarsList()
{
    if (_AuthResult == null)
        return;

    try
    {
        _graphClient = new GraphServiceClient(
                 new DelegateAuthenticationProvider(
                     (requestMessage) =>
                     {
                         // Append the access token to the request.
                         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _AuthResult.AccessToken);
                         return Task.FromResult(0);
                     }));

        var calendars = await _graphClient
                             .Me
                             .Calendars
                             .Request()
                             .GetAsync();


        Console.WriteLine($"Number of calendars: {calendars.Count}");

        _ListCalendars = calendars.ToList();

        foreach (Calendar oCalendar in _ListCalendars)
        {
            var oEvents = await _graphClient
                                .Me
                                .Calendars[oCalendar.Id]
                                .Events
                                .Request()
                                .GetAsync();
            if(oEvents?.Count > 0)
            {
                foreach(Event oEvent in oEvents)
                {
                    string strEventInfo = $"Subject: {oEvent.Subject}" + Environment.NewLine;
                    strEventInfo += $"Body: {oEvent.Body}" + Environment.NewLine;
                    strEventInfo += $"Times: Start - {oEvent.Start} End - {oEvent.End}" + Environment.NewLine;
                    strEventInfo += $"Location: {oEvent.Location}" + Environment.NewLine;
                    strEventInfo += $"All Day: {oEvent.IsAllDay}" + Environment.NewLine;
                    Console.WriteLine(strEventInfo);
                }
            }
        }

    }
    catch (Exception ex)
    {
        _ListCalendars = null;
        Console.WriteLine($"Error BuildCalendarsList: {ex.Message}");
    }
}

为什么我必须这样做?

理想情况下,我希望调整此方法:

private async Task Run()
{
    Outlook oOutlook = new Outlook();
    await oOutlook.AquireToken();
    if(oOutlook.AuthResult == null)
    {
        if (oOutlook.ResultsText != "")
            Console.WriteLine(oOutlook.ResultsText);
    }
    else
    {
        oOutlook.DisplayBasicTokenInfo();

        await oOutlook.BuildCalendarsList();
        oOutlook.DisplayCalendarsList(false);
        //oOutlook.DisplayCalendarsList(true);

        oOutlook.SignOut();
    }
}

因此,如果我通过true,它将提供详细的事件输出。

我很欣赏有关如何将BuildCalendarsList分解为两种方法的建议。

1 个答案:

答案 0 :(得分:1)

events集合可能很难处理。它是基于规则的基础数据的原始视图。例如,定期会议有一个“主”事件和一个例外列表(移动日期/时间,取消发生等)。 events集合通常不应用于获取某人日历的“视图”。

而不是events,请查看calendarView。这为您提供了日历的“渲染”视图(即用户通常在Outlook中看到的内容)。