简体   繁体   English

使用Exchange Web服务API提取Exchange 2007公共日历约会

[英]Extract Exchange 2007 Public Calendar Appointments using Exchange Web Services API

We have a public calendar for our company set up in an Exchange 2007 Public Folder. 我们在Exchange 2007公用文件夹中为我们的公司设置了公用日历。 I am able to retrieve my personal calendar appointments for the current day using the code below. 我可以使用以下代码检索当天的个人日历约会。 I have searched high and low online and I cannot find one example of someone retrieving calendar information from a Public Folder calendar. 我已经在网上搜索了高低两档,但找不到一个有人从“公用文件夹”日历中检索日历信息的示例。

It seems like it should be doable, but I cannot for the life of me get it working. 看起来这应该可行,但是我无法一辈子都可以使用它。 How can I modify the code below to access the calendar? 如何修改下面的代码以访问日历? I am not interested in creating any appointments through asp.net, just retrieving a simple list. 我对通过asp.net创建任何约会不感兴趣,只是检索一个简单列表。 I am open to any other suggestions as well. 我也欢迎其他任何建议。 Thanks. 谢谢。

ADDED BOUNTY 额外奖励
- I can't be the only person that ever needed to do this. -我不可能是唯一需要这样做的人。 Let's get this problem solved for future generations. 让我们为子孙后代解决这个问题。

UPDATED AGAIN DUE TO IGNORANCE 由于无知而再次更新
- I failed to mention that the project I am working on is .NET 2.0 (very important don't you think?). -我没有提到我正在从事的项目是.NET 2.0(非常重要,您认为吗?)。

* ADDED MY CODE SOLUTION BELOW * *下面添加了我的代码解决方案*
- I have replaced my original code example with the code that ended up working. -我将原来的代码示例替换为最终可用的代码。 Many thanks to Oleg for providing the code to find the public folder, which was the hardest part.. I have modified the code using the example from here http://msexchangeteam.com/archive/2009/04/21/451126.aspx to use the simpler FindAppointments method. 非常感谢Oleg提供的代码来查找公用文件夹,这是最难的部分。.我使用此处的示例修改了代码, 网址为http://msexchangeteam.com/archive/2009/04/21/451126.aspx使用更简单的FindAppointments方法。

This simple example returns an html string with the appointments, but you can use it as a base to customize as needed. 这个简单的示例返回带有约会的html字符串,但是您可以将其用作根据需要进行自定义的基础。 You can see our back and forth under his answer below. 您可以在下面的回答中看到我们的来回信息。

using System;
using Microsoft.Exchange.WebServices.Data;
using System.Net;

namespace ExchangePublicFolders
{
    public class Program
    {
        public static FolderId FindPublicFolder(ExchangeService myService, FolderId baseFolderId,
        string folderName)
        {

        FolderView folderView = new FolderView(10, 0);
        folderView.OffsetBasePoint = OffsetBasePoint.Beginning;
        folderView.PropertySet = new PropertySet(FolderSchema.DisplayName, FolderSchema.Id);

        FindFoldersResults folderResults;
        do
        {
            folderResults = myService.FindFolders(baseFolderId, folderView);

            foreach (Folder folder in folderResults)
                if (String.Compare(folder.DisplayName, folderName, StringComparison.OrdinalIgnoreCase) == 0)
                    return folder.Id;

            if (folderResults.NextPageOffset.HasValue)
                folderView.Offset = folderResults.NextPageOffset.Value;
        }
        while (folderResults.MoreAvailable);

        return null;
    }

    public static string MyTest()
    {
        ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

        myService.Credentials = new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
        myService.Url = new Uri("https://MAILSERVER/ews/exchange.asmx");

        Folder myPublicFoldersRoot = Folder.Bind(myService, WellKnownFolderName.PublicFoldersRoot);
        string myPublicFolderPath = @"PUBLIC_FOLDER_CALENDAR_NAME";
        string[] folderPath = myPublicFolderPath.Split('\\');
        FolderId fId = myPublicFoldersRoot.Id;
        foreach (string subFolderName in folderPath)
        {
            fId = Program.FindPublicFolder(myService, fId, subFolderName);
            if (fId == null)
            {
                return string.Format("ERROR: Can't find public folder {0}", myPublicFolderPath);

            }
        }

        Folder folderFound = Folder.Bind(myService, fId);
        if (String.Compare(folderFound.FolderClass, "IPF.Appointment", StringComparison.Ordinal) != 0)
        {
            return string.Format("ERROR: Public folder {0} is not a Calendar", myPublicFolderPath);

        }

        CalendarFolder AK_Calendar = CalendarFolder.Bind(myService, fId, BasePropertySet.FirstClassProperties);

        FindItemsResults<Appointment> AK_appointments = AK_Calendar.FindAppointments(new CalendarView(DateTime.Now,DateTime.Now.AddDays(1)));


        string rString = string.Empty;


        foreach (Appointment AK_appoint in AK_appointments)
        {

            rString += string.Format("Subject: {0}<br />Date: {1}<br /><br />",  AK_appoint.Subject, AK_appoint.Start);    
        }

        return rString;
    }

    }
}

Like promised here is a code example. 像这里承诺的是一个代码示例。 I used the Microsoft Exchange Web Services (EWS) Managed API 1.0 and recommend you to do the same. 我使用了Microsoft Exchange Web服务(EWS)托管API 1.0 ,建议您这样做。 The most comments I included in the code 我在代码中包含的最多评论

using System;
using Microsoft.Exchange.WebServices.Data;
using System.Net;

namespace ExchangePublicFolders {
    class Program {
        static FolderId FindPublicFolder (ExchangeService myService, FolderId baseFolderId,
            string folderName) {

            // We will search using paging. We will use page size 10
            FolderView folderView = new FolderView (10,0);
            folderView.OffsetBasePoint = OffsetBasePoint.Beginning;
            // we will need only DisplayName and Id of every folder
            // se we'll reduce the property set to the properties
            folderView.PropertySet = new PropertySet (FolderSchema.DisplayName,
                FolderSchema.Id);

            FindFoldersResults folderResults;
            do {
                folderResults = myService.FindFolders (baseFolderId, folderView);

                foreach (Folder folder in folderResults)
                    if (String.Compare (folder.DisplayName, folderName, StringComparison.OrdinalIgnoreCase) == 0)
                        return folder.Id;

                if (folderResults.NextPageOffset.HasValue)
                    // go to the next page
                    folderView.Offset = folderResults.NextPageOffset.Value;
            }
            while (folderResults.MoreAvailable);

            return null;
        }

        static void MyTest () {
            // IMPORTANT: ExchangeService is NOT thread safe, so one should create an instance of
            // ExchangeService whenever one needs it.
            ExchangeService myService = new ExchangeService (ExchangeVersion.Exchange2007_SP1);

            myService.Credentials = new NetworkCredential ("MyUser@corp.local", "myPassword00");
            myService.Url = new Uri ("http://mailwebsvc-t.services.local/ews/exchange.asmx");
            // next line is very practical during development phase or for debugging
            myService.TraceEnabled = true;

            Folder myPublicFoldersRoot = Folder.Bind (myService, WellKnownFolderName.PublicFoldersRoot);
            string myPublicFolderPath = @"OK soft GmbH (DE)\Gruppenpostfächer\_Template - Gruppenpostfach\_Template - Kalender";
            string[] folderPath = myPublicFolderPath.Split('\\');
            FolderId fId = myPublicFoldersRoot.Id;
            foreach (string subFolderName in folderPath) {
                fId = FindPublicFolder (myService, fId, subFolderName);
                if (fId == null) {
                    Console.WriteLine ("ERROR: Can't find public folder {0}", myPublicFolderPath);
                    return;
                }
            }

            // verify that we found 
            Folder folderFound = Folder.Bind (myService, fId);
            if (String.Compare (folderFound.FolderClass, "IPF.Appointment", StringComparison.Ordinal) != 0) {
                Console.WriteLine ("ERROR: Public folder {0} is not a Calendar", myPublicFolderPath);
                return;
            }

            CalendarFolder myPublicFolder = CalendarFolder.Bind (myService,
                //WellKnownFolderName.Calendar,
                fId,
                PropertySet.FirstClassProperties);

            if (myPublicFolder.TotalCount == 0) {
                Console.WriteLine ("Warning: Public folder {0} has no appointment. We try to create one.", myPublicFolderPath);

                Appointment app = new Appointment (myService);
                app.Subject = "Writing a code example";
                app.Start = new DateTime (2010, 9, 9);
                app.End = new DateTime (2010, 9, 10);
                app.RequiredAttendees.Add ("oleg.kiriljuk@ok-soft-gmbh.com");
                app.Culture = "de-DE";
                app.Save (myPublicFolder.Id, SendInvitationsMode.SendToNone);
            }

            // We will search using paging. We will use page size 10
            ItemView viewCalendar = new ItemView (10);
            // we can include all properties which we need in the view
            // If we comment the next line then ALL properties will be
            // read from the server. We can see there in the debug output
            viewCalendar.PropertySet = new PropertySet (ItemSchema.Subject);
            viewCalendar.Offset = 0;
            viewCalendar.OffsetBasePoint = OffsetBasePoint.Beginning;
            viewCalendar.OrderBy.Add (ContactSchema.DateTimeCreated, SortDirection.Descending);

            FindItemsResults<Item> findResultsCalendar;
            do {
                findResultsCalendar = myPublicFolder.FindItems (viewCalendar);

                foreach (Item item in findResultsCalendar) {
                    if (item is Appointment) {
                        Appointment appoint = item as Appointment;
                        Console.WriteLine ("Subject: \"{0}\"", appoint.Subject);
                    }
                }

                if (findResultsCalendar.NextPageOffset.HasValue)
                    // go to the next page
                    viewCalendar.Offset = findResultsCalendar.NextPageOffset.Value;
            }
            while (findResultsCalendar.MoreAvailable);
        }
        static void Main (string[] args) {
            MyTest();
        }
    }
}

You should update the string myPublicFolderPath to the value with your public calender folder. 您应该使用公共日历文件夹将字符串myPublicFolderPath更新为该值。 I set myService.TraceEnabled = true which produce long output with debug information. 我设置myService.TraceEnabled = true会产生带有调试信息的长输出。 You should of cause remove the line for production. 您应该删除生产线。

UPDATED : Some additional links you could find in Create new calendar system support in Exchange OWA . 更新在Exchange OWA中的“ 创建新的日历系统支持”中可以找到一些其他链接。 If you not yet seen the videos and you want to use Exchange Web Services I would recommend you to watch there. 如果您尚未看过视频,并且想使用Exchange Web Services,建议您在此观看。 It could save your time in the future. 这样可以节省您将来的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM