冰恋 发表于 2015-9-11 08:58:52

exchange webservice访问类(日程新增和删除)

  using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using HPCalendarSyncToPT1.Common;
using HPCalendarSyncToPT1.Exchange;
  namespace HPCalendarSyncToPT1
{
    public class ExchangeWebService
    {
      ExchangeServiceBinding esb = null;
      public ExchangeWebService(string account, string password, string domain)
      {
            esb = new ExchangeServiceBinding();            
            esb.Credentials = new NetworkCredential(account, password, domain);
            esb.Url = ConfigurationManager.AppSettings["pt1exchange_ws_url"];
      }
  public bool DeleteItem(string itemId, string changeKey)
      {
            bool result = false;
            DeleteItemType d = new DeleteItemType();
            ItemIdType id = new ItemIdType();
            id.Id = itemId;
            id.ChangeKey = changeKey;
            d.ItemIds = new ItemIdType[] {id};
            d.SendMeetingCancellations = CalendarItemCreateOrDeleteOperationType.SendToNone;
            d.SendMeetingCancellationsSpecified = true;
            DeleteItemResponseType deleteItemResponse = esb.DeleteItem(d);
  if (deleteItemResponse != null)
            {
                if (deleteItemResponse.ResponseMessages.Items.ResponseClass == ResponseClassType.Error)
                {
                  Console.WriteLine(deleteItemResponse.ResponseMessages.Items.MessageText);
                }
                else
                {
                  Console.WriteLine("删除日程成功!");
                  result = true;
                }
            }
            return result;
      }
  public ExchangeCalendarItemResult CreateAppointmentEWS(Calendar item)
      {
            // Create the appointment.
            CalendarItemType appointment = new CalendarItemType();
  // Add item properties to the appointment.
            appointment.Body = new BodyType();
            appointment.Body.BodyType1 = BodyTypeType.Text;
            appointment.Body.Value = "";//正文内容
            //appointment.Categories = new string[] { "aaa","bbb" };//类别
            appointment.Importance = ImportanceChoicesType.High;
            appointment.ImportanceSpecified = true;
            appointment.ItemClass = "IPM.Appointment";
            appointment.Subject = item.Subject;//标题
  // Add calendar properties to the appointment.
            appointment.Start = item.StartTime;
            appointment.StartSpecified = true;
            appointment.End = item.EndTime;
            appointment.EndSpecified = true;
            //appointment.HasAttachments = item.HasAttachment;
  
  // Identify the destination folder that will contain the appointment.
            DistinguishedFolderIdType folder = new DistinguishedFolderIdType();
            folder.Id = DistinguishedFolderIdNameType.calendar;
  // Create the array of items that will contain the appointment.
            NonEmptyArrayOfAllItemsType arrayOfItems = new NonEmptyArrayOfAllItemsType();
            arrayOfItems.Items = new ItemType;
  // Add the appointment to the array of items.
            arrayOfItems.Items = appointment;
  // Create the CreateItem request.
            CreateItemType createItemRequest = new CreateItemType();
  // The SendMeetingInvitations attribute is required for calendar items.
            createItemRequest.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone;
            createItemRequest.SendMeetingInvitationsSpecified = true;
  // Add the destination folder to the CreateItem request.
            createItemRequest.SavedItemFolderId = new TargetFolderIdType();
            createItemRequest.SavedItemFolderId.Item = folder;
  // Add the items to the CreateItem request.
            createItemRequest.Items = arrayOfItems;
  ExchangeCalendarItemResult r = null;
            try
            {
                // Send the request and get the response.
                CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);
  // Get the response messages.
                ResponseMessageType[] rmta = createItemResponse.ResponseMessages.Items;
  foreach (ResponseMessageType rmt in rmta)
                {
                  ArrayOfRealItemsType itemArray = ((ItemInfoResponseMessageType)rmt).Items;
                  ItemType[] items = itemArray.Items;
  // Get the item identifier and change key for each item.
                  r = new ExchangeCalendarItemResult();
                  if (items != null)
                  foreach (ItemType itm in items)
                  {
                        Console.WriteLine("Item identifier: " + itm.ItemId.Id);
                        Console.WriteLine("Item change key: " + itm.ItemId.ChangeKey);
                        r.ItemId = itm.ItemId.Id;
                        r.ItemKey = itm.ItemId.ChangeKey;
                  }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Message: " + e.Message);
            }
            return r;
      }
    }
}
页: [1]
查看完整版本: exchange webservice访问类(日程新增和删除)