如何使用EWS托管API访问共享日历

时间:2016-12-12 07:27:02

标签: exchangewebservices

如何使用Exchange EWS API仅检索共享日历。

我如何知道使用Exchange EWS API共享哪个日历。

1 个答案:

答案 0 :(得分:2)

您只需在FolderId中指定要访问的日历即可。

FolderId folderIdFromCalendar = new FolderId(WellKnownFolderName.Calendar, "sharedInbox@example.com");

然后,您可以使用folderIdFromCalendar来检索您想要的日历项目,例如:

FindItemsResults<Item> appointments = ExchangeServive.FindItems(folderIdFromCalendar, ItemView);

请记住,程序必须在共享邮箱的用户的上下文中运行。

更新

如果您碰巧知道FolderId,则可以通过执行此代码检查当前用户是否有权访问此日历:

如上所述,您需要初始化FolderId

FolderId folderIdFromCalendar = new FolderId("AQMkADAwATM3ZmYAZS1kNjE0LWU1ZmQtMDACLTAwCgAuAAADJRMtiupYBUGD‌​cKdcqUrr3AEA1/sqwbrw‌​UEeFM0Mc+UBFoQAAABzk‌​m1sAAAA=");

之后,请尝试以下代码:

if (folderIdFromCalendar != null)
    {
        try
        {
            ItemView cView = new ItemView(1000);
            FindItemsResults<Item> appointments = service.FindItems(folderIdFromCalendar, cView);
            int count = appointments.TotalCount; //just an example of some random action on the folder calendar
        }
        catch (ServiceResponseException ex)
        {
            Console.WriteLine("The specified calendar was not shared with you. \n" + ex);
        }
    }
    else
    {
        Console.WriteLine("The specified calendar ID could not be linked to a calendar folder.");
    }

另一个更新:

if (folderIdFromCalendar != null)
    {
        if (folderIdFromCalendar.Mailbox.ToString().ToLower() == "your-Mailadress".ToLower())
        {
            Console.WriteLine("The folder you specified is your own");
        }
        else
        {
            try
            {
                ItemView cView = new ItemView(1000);
                FindItemsResults<Item> appointments = service.FindItems(folderIdFromCalendar, cView);
                int count = appointments.TotalCount; //just an example of some random action on the folder calendar
                Console.WriteLine("You have been given access to this mailbox. Its owner is: " + folderIdFromCalendar.Mailbox.ToString()); //if you need to know, whos mailbox you are accessing right now
            }
            catch (ServiceResponseException ex)
            {
                Console.WriteLine("The specified calendar was not shared with you. \n" + ex);
            }
        }
    }
    else
    {
        Console.WriteLine("The specified calendar ID could not be linked to a calendar folder.");
    }

最后更新:

我现在搜索了一下,发现了一些对我有用的东西。但说实话,我并不了解所有内容,这些内容正如下面的代码所示:

FolderId folderIdFromCalendar = new FolderId("AQMkADAwATM3ZmYAZS1kNjE0LWU1ZmQtMDACLTAwCgAuAAADJRMtiupYBUGD‌​cKdcqUrr3AEA1/sqwbrw‌​UEeFM0Mc+UBFoQAAABzk‌​m1sAAAA=");
    Folder folderFromCalendar = Folder.Bind(service, folderIdFromCalendar);

    AlternateId aiAlternateid = new AlternateId(IdFormat.EwsId, folderFromCalendar.Id.UniqueId, "random@mailadress.com");
    AlternateIdBase aiResponse = service.ConvertId(aiAlternateid, IdFormat.EwsId);

    var mailbox = ((AlternateId)aiResponse).Mailbox;

    if (folderFromCalendar != null)
    {
        if (mailbox.ToString().ToLower() == "your-Mailadress".ToLower())
        {
            Console.WriteLine("The folder you specified is your own");
        }
        else
        {
            try
            {
                ItemView cView = new ItemView(1000);
                FindItemsResults<Item> appointments = service.FindItems(folderIdFromCalendar, cView);
                int count = appointments.TotalCount; //just an example of some random action on the folder calendar
                Console.WriteLine("You have been given access to this mailbox. Its owner is: " + mailbox.ToString());
            }
            catch (ServiceResponseException ex)
            {
                Console.WriteLine("The specified calendar was not shared with you. \n" + ex);
            }
        }
    }
    else
    {
        Console.WriteLine("The specified calendar ID could not be linked to a calendar folder.");
    }

我不明白,字符串&#34; random@mailadress.com"影响该程序中的任何内容,但语法需要一个字符串。