• 使用Outlook EWS获取会议及会议室信息


    1、建立连接:

    //与exchange web服务器建立连接
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
    ExchangeCredentials credentials = new WebCredentials([邮箱账号], [密码]);
    service.Credentials = credentials;
    //这个url可能每个公司不一样,大多数是这个
    service.Url = new Uri("https://outlook.[域名].com/ews/exchange.asmx");
    service.TraceEnabled = true;

    2、获取会议列表:

    //Initialize values for the start and end times, and the number of appointments to retrieve.
    DateTime startDate = DateTime.Now.AddDays(-1).Date.AddSeconds(1).AddDays(1);
    DateTime endDate = startDate.AddDays(1);
    const int NUM_APPTS = 5;
    // Initialize the calendar folder object with only the folder ID. 
    CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
    // Set the start and end time and number of appointments to retrieve.
    CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);
    // Limit the properties returned to the appointment's subject, start time, and end time.
    cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Location, AppointmentSchema.Organizer);
    // Retrieve a collection of appointments by using the calendar view.
    FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
    Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
                                  " to " + endDate.Date.ToShortDateString() + " are: \n");
    
    foreach (Appointment a in appointments)
    {
      Console.Write("Subject: " + a.Subject.ToString() + " ");
      Console.Write("Start: " + a.Start.ToString() + " ");
      Console.Write("End: " + a.End.ToString());
      if (a.Location != null)
      {
        Console.Write("End: " + a.Location.ToString());
      }
      Console.WriteLine();
    }

    3、获取会议室列表:

    try
    {
      // 这里先获取大的会议室列表,大多数是按区域分的,不如公司有五个办公地点,这里获取的就是各个办公地点的address
      EmailAddressCollection myRoomLists = service.GetRoomLists();
      List<String> addList = new List<String>();
      foreach (EmailAddress address in myRoomLists)
      {
        EmailAddress emailAddress = new EmailAddress(address.Address);
        // 根据各个办公地点的address,再获取具体的所有会议室信息
        // 如获取北京办公地的所有会议室信息
        Collection<EmailAddress> list = service.GetRooms(emailAddress);
        foreach (EmailAddress e in list)
        {
          string aa = e.Address;
          string bb = e.Name;
        }
      }
    }
    catch (Exception e1)
    {
      // TODO Auto-generated catch block
      //e1.printStackTrace();
    }

    4、获取会议室事件:

    //读取会议室预定情况
    List<AttendeeInfo> attendees = new List<AttendeeInfo>();
    attendees.Add(new AttendeeInfo("[会议室邮箱地址]", MeetingAttendeeType.Room, true));
    GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
    // 设置当天时间
    new TimeWindow(DateTime.Now.AddDays(-3).Date, DateTime.Now.AddDays(1).Date), AvailabilityData.FreeBusyAndSuggestions);
    
    List<Hashtable> htList = new List<Hashtable>();
    
    foreach (AttendeeAvailability availability in results.AttendeesAvailability)
    {
      foreach (CalendarEvent calEvent in availability.CalendarEvents)
      {
        Hashtable ht = new Hashtable();
        // 开始时间和结束时间
        ht.Add("start", calEvent.StartTime);
        ht.Add("end", calEvent.EndTime);
        CalendarEventDetails details = calEvent.Details;
    
        if (details != null)
        {
          ht.Add("subject", details.Subject);
        }
    
        htList.Add(ht);
      }
    }

     5、获取会议室日历:

    CalendarView testCView = new CalendarView(calEvent.StartTime, calEvent.EndTime);
    FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox("[会议室邮箱列表]"));
    FindItemsResults<Appointment> findResults = null;
    try
    {
      findResults = service.FindAppointments(folderId, testCView);
      List<Appointment> appointmentItems = findResults.Items.ToList();
    
      foreach (Appointment ap in appointmentItems)
      {
        string name = ap.Organizer.Name;
        string address = ap.Organizer.Address;
      }
    }
    catch (Exception e)
    {
    
    }

    6、默认返回的Organizer.Address格式为:

    /o=[域名]/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=a272ba5365f94925982d3b1a3117444d-[域账号]

    如果要显示邮箱地址,需要重新加载Organizer.Address属性值:

    //Calendar View
    CalendarView testCView = new CalendarView(calEvent.StartTime, calEvent.EndTime);
    FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox("[会议室邮箱地址]"));
    FindItemsResults<Appointment> findResults = null;
    try
    {
      findResults = service.FindAppointments(folderId, testCView);
    
      PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties);
      propertySet.RequestedBodyType = BodyType.Text;
      service.LoadPropertiesForItems(from Item item in findResults select item, propertySet);
    
      List<Appointment> appointmentItems = findResults.Items.ToList();
    
      foreach (Appointment ap in appointmentItems)
      {
        string name = ap.Organizer.Name;
        string address = ap.Organizer.Address;
      }
    }
    catch (Exception e)
    {
    
    }

     BTW,返回的会议主题可能会变成组织者姓名,与会议室的配置有关系,通过PowerShell命令修改即可,已验证成功。

    Set-CalendarProcessing -Identity [会议室域账号] -AddOrganizerToSubject $false -DeleteSubject $false

    参考文档:

    https://stackoverflow.com/questions/24446558/when-retrieving-an-appointment-with-ews-the-subject-contains-the-organizer-name

    https://www.slipstick.com/exchange/cmdlets/meeting-organizers-name-appears-in-subject-line/

    https://docs.microsoft.com/en-us/powershell/module/exchange/set-calendarprocessing?view=exchange-ps

  • 相关阅读:
    C语言I博客作业02
    第一次作业
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业03
    C语言I博客作业02
    课程目标
    具体方面
  • 原文地址:https://www.cnblogs.com/61007257Steven/p/15825465.html
Copyright © 2020-2023  润新知