无名

长风破浪会有时,直挂云帆济沧海!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

the example code updated the calendar in exchange server 2007(or Later)  by web services method.

 

 

  1 private static WebApplication2.EWS.ExchangeServiceBinding ExchangeSevicesBinding()
  2        {
  3            WebApplication2.EWS.ExchangeServiceBinding binding = new WebApplication2.EWS.ExchangeServiceBinding();
  4            binding.Credentials = new System.Net.NetworkCredential("username""password""domain");
  5            binding.Url = https://mail.abc.com/ews/exchange.asmx;
  6            return binding;
  7        }

  8    
  9  
 10   public static ItemIdType UpdateMessageBody()
 11        {
 12            ExchangeServiceBinding binding = ExchangeSevicesBinding();
 13
 14            UpdateItemType request = new UpdateItemType();
 15
 16            request.ConflictResolution = ConflictResolutionType.AutoResolve;
 17
 18            request.MessageDisposition = MessageDispositionType.SaveOnly;
 19
 20            request.MessageDispositionSpecified = true;
 21
 22            // Let's create our item that will hold the changed body. Remember, 
 23
 24            // we can only set a single property for each change description.
 25
 26            //
 27
 28            MessageType myMessage = new MessageType();
 29
 30            myMessage.Body = new BodyType();
 31
 32            myMessage.Body.BodyType1 = BodyTypeType.Text;
 33
 34            myMessage.Body.Value = "update calendar event";
 35
 36            // Now, create our change description using our handy overloaded constructor
 37            CalendarItemType calendarItemTypeItem = GetCalenderItem();
 38
 39            //
 40            update Attendees
 58
 59
 60            update body
 71
 72            update body

 73            CalendarItemType calendarItemType = new CalendarItemType();
 74            calendarItemType.StartSpecified = true;
 75            calendarItemType.Start = calendarItemTypeItem.Start.AddHours(1);
 76           // calendarItemType.End = calendarItemTypeItem.End.AddHours(1);
 77            //calendarItemType.RequiredAttendees = new AttendeeType[3];
 78            //calendarItemType.RequiredAttendees[0] = new AttendeeType();
 79            //calendarItemType.RequiredAttendees[0].Mailbox = new EmailAddressType();
 80            //calendarItemType.RequiredAttendees[0].Mailbox.EmailAddress = "wu_zaijun@163.com";
 81            //calendarItemType.RequiredAttendees[1] = new AttendeeType();
 82            //calendarItemType.RequiredAttendees[1].Mailbox = new EmailAddressType();
 83            //calendarItemType.RequiredAttendees[1].Mailbox.EmailAddress = "wuzj@heng-zhun.com";
 84            //calendarItemType.RequiredAttendees[2] = new AttendeeType();
 85            //calendarItemType.RequiredAttendees[2].Mailbox = new EmailAddressType();
 86            //calendarItemType.RequiredAttendees[2].Mailbox.EmailAddress = "ahwzj2008@163.com";
 87            SetItemFieldType setItem = new SetItemFieldType();
 88            PathToUnindexedFieldType bodyPath = new PathToUnindexedFieldType();
 89            bodyPath.FieldURI = UnindexedFieldURIType.calendarStart;
 90            setItem.Item = bodyPath;
 91            setItem.Item1 = calendarItemType;
 92            #endregion

 93
 94            // The change description lives within an ItemChange
 95            //
 96            ItemChangeType itemChange = new ItemChangeType();
 97
 98            itemChange.Item = calendarItemTypeItem.ItemId;
 99
100            itemChange.Updates = new ItemChangeDescriptionType[] { setItem };
101
102            // Now set our single item change on the request
103            //
104            request.ItemChanges = new ItemChangeType[] { itemChange };
105            request.SendMeetingInvitationsOrCancellationsSpecified = true;
106            request.SendMeetingInvitationsOrCancellations = CalendarItemUpdateOperationType.SendOnlyToChanged;
107            UpdateItemResponseType response = binding.UpdateItem(request);
108
109            // UpdateItem gives us back ItemInfoResponseMessageType instances. 
110            // All it gives us, however, is the id of the item.
111            //
112            ItemInfoResponseMessageType responseMessage =
113
114                response.ResponseMessages.Items[0as ItemInfoResponseMessageType;
115
116            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
117            {
118
119                return responseMessage.Items.Items[0].ItemId;
120
121            }

122
123            else
124            {
125
126                throw new Exception("Error trying to update the item: ResponseCode: " +
127
128                  responseMessage.ResponseCode);
129
130            }

131
132        }
133 public static CalendarItemType GetCalenderItem()
134        {
135            WebApplication2.EWS.ExchangeServiceBinding binding = ExchangeSevicesBinding();
136
137          
138            CalendarViewType calendarView = CreateCalendarViewForToday(DateTime.Now, DateTime.Now.AddHours(8));
139
140            // Prepare the Find Item Request
141            //
142            FindItemType findItemRequest = new FindItemType();
143
144            // Set the ID(s) of the Find Item Request to be the Distinguished
145            // Folder, 'calendar'.
146            //
147            findItemRequest.ParentFolderIds = new DistinguishedFolderIdType[] new DistinguishedFolderIdType() };
148            ((DistinguishedFolderIdType)findItemRequest.ParentFolderIds[0]).Id =
149                DistinguishedFolderIdNameType.calendar;
150
151            findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
152
153            // Prepare an Item shape type that defines how the items in view will be
154            // returned. This could easily be extended with additional properties to
155            // be supplied by the caller
156            //
157            ItemResponseShapeType itemShapeDefinition = new ItemResponseShapeType();
158            itemShapeDefinition.BaseShape = DefaultShapeNamesType.AllProperties;
159
160            findItemRequest.ItemShape = itemShapeDefinition;
161            findItemRequest.Item = calendarView;
162
163            // Send the request via the Exchange Service Binding
164            FindItemResponseType findItemResponse =
165                binding.FindItem(findItemRequest);
166
167            // Verify that the FindItem request was successfull
168            if (findItemResponse.ResponseMessages.Items[0].ResponseClass !=
169                ResponseClassType.Success)
170            {
171                // Indicate that we have a problem
172                throw new Exception(String.Format(
173                    "Unable to get calendar view\r\n{0}\r\n{1}",
174                    findItemResponse.ResponseMessages.Items[0].ResponseCode,
175                    findItemResponse.ResponseMessages.Items[0].MessageText));
176            }

177
178            FindItemResponseMessageType findItemResponseMessage =
179            (FindItemResponseMessageType)findItemResponse.
180                ResponseMessages.Items[0];
181            ArrayOfRealItemsType findItemResponseItems =
182                (ArrayOfRealItemsType)findItemResponseMessage.RootFolder.Item;
183            for (int x = 0;
184             x < findItemResponseMessage.RootFolder.TotalItemsInView;
185             x++)
186            {
187               
188               return   (CalendarItemType)findItemResponseItems.Items[x];
189
190            }

191            return null;
192
193        }

you must special attention to the red  bold text .

if you update other item in exchange server,you can consult the follow xml style.

 

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3                xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 4   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
 5   xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
 6   <soap:Body>
 7     <UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve" 
 8                 xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
 9       <ItemChanges>
10         <t:ItemChange>
11           <t:ItemId Id="AAAtAEFkbW" ChangeKey="CQAAABYA"/>
12           <t:Updates>
13             <t:AppendToItemField>
14               <t:FieldURI FieldURI="item:Body"/>
15               <t:Message>
16                 <t:Body BodyType="Text">Some additional text to append</t:Body>
17               </t:Message>
18             </t:AppendToItemField>
19           </t:Updates>
20         </t:ItemChange>
21       </ItemChanges>
22     </UpdateItem>
23   </soap:Body>
24 </soap:Envelope>
25 

 

posted on 2009-06-18 11:43  无名  阅读(834)  评论(0编辑  收藏  举报