Exchange uses EWS managed API2.0 to synchronize mailboxes

Source: Internet
Author: User

You can retrieve items from a list of changes in a folder from a given point in time by using the Exchange Web Serivice (EWS) managed API.

The client can use the Syncfoldersitems method to synchronize the project on the server side, you just have to follow the below to do it:

      • Performs an initial synchronization operation (to retrieve a list of all items in the specified file home).
      • Periodically performs subsequent synchronization operations to retrieve a list of item changes that have occurred since the previous synchronization.

In order to synchronize each item on the server, the client handles changes to the collection returned by the method SyncFolderItems method and applies these changes to each item on-premises.

The SyncFolderItems method is a one-way synchronization of the server to client. To back up the client's changes to the server side, the client must use other methods of the EWS managed API to create, update, and delete the necessary items.

Note

The Sysncfolderitems method returns a maximum of 512 changes, and then the SyncFolderItems request must be subject to additional changes.

The SyncFolderItems method is similar to the FindItem method, but it does not return the body and attachments properties. If you need these attributes, you cannot use SyncFolderItems, we recommend that you specify the Idonly property when you call the SyncFolderItems method, and then use the Loadpropertiesforitems method to get the properties you want.

Perform the initial synchronization operation

1. Call the Sysncfolderitems method and specify the following: The folder containing the synchronized data, the property will return each item in response, the IDs corresponding to the different steps of the data should return the settings of the project, a change should return the maximum number of integers, A Sysncfolderitems enumeration value indicates the type of item that the synchronization data should return, and an empty synchronization state. Specifies an empty synchronization state that causes the Sysncfolderitems method to return a collection that represents all items in the specified folder that meet the conditions specified by other input parameters. The following code shows how to request a list of all the normal items that are contained in the Inbox folder (up to 512 items), and each folder in the response returns the first class of property sets. The connection configuration information is provided by using a named Exchangeservice for the service object.

changecollection<itemchange> ICC = service. SyncFolderItems (newnull,null);

2. Save the synchronization state for use with the next SyncFolderItems method call. The following example shows how to access the Syncstate state, which is used by the client to store the value, and then when the SyncFolderItems method is called.

string ssyncstate = ICC. Syncstate;

3. If the SyncFolderItems method returns a list of changes, traverse the list.

if(ICC. Count = =0) {Console.WriteLine ("There is no items to synchronize.");}Else{    foreach(ItemChange ICinchICC) {Console.WriteLine ("changetype:"+IC.        Changetype.tostring ()); Console.WriteLine ("ItemId:"+IC.        Itemid.uniqueid); Console.WriteLine ("Subject:"+IC.        Item.Subject); Console.WriteLine ("==========="); //todo:create item on the client.    }}

Perform the subsequent synchronization operation

1. Call the SyncFolderItems method and specify the following: The folder containing the synchronized data, the property will return each item in response, the IDs corresponding to the different steps of the data should return the settings of the project, a change should return the maximum number of integers, A Syncfolderitemsscope enumeration value indicates that the synchronization data should return the type of item, and the synchronization state value from the existing synchronization response. The following code shows how to request a list of all changes to a normal item, contained in the Inbox folder (up to 512 changes), from the time that corresponds to the specified synchronization state, and the first class property set is returned as a response in each folder. The connection configuration information is provided by using a named Exchangeservice for the service object.

changecollection<itemchange> ICC = service. SyncFolderItems (newnull, Syncfolderitemsscope.normalitems, ssyncstate);

2. If the SyncFolderItems method returns a changed list, enumerates the list and processes it on the client.

if(ICC. Count = =0) {Console.WriteLine ("There is no item changes to synchronize.");}Else{    foreach(ItemChange ICinchICC) {if(IC. ChangeType = =changetype.create) {//todo:create item on the client.        }        Else if(IC. ChangeType = =changetype.update) {//todo:update item on the client.        }        Else if(IC. ChangeType = =changetype.delete) {//Todo:delete item on the client.        }        Else if(IC. ChangeType = =changetype.readflagchange) {//todo:update The item ' s read flag on the client.} Console.WriteLine ("changetype:"+IC.        Changetype.tostring ()); Console.WriteLine ("ItemId:"+IC.        Itemid.uniqueid); if(IC. Item! =NULL) {Console.WriteLine ("Subject:"+IC.        Item.Subject); } Console.WriteLine ("==========="); }}

Example

The following code example shows how to get a list of all changes in the Inbox, which occurs by Ssyncstate. This change is retrieved in five batches, using the syncfolderitems of successive invocation methods until no more changes have been reached. This example assumes that the service is a valid Exchangeservice combination, Ssyncstate represents the synchronization state, and the SyncFolderItems is called before returning.

//Initialize the flag that would indicate when there is no more changes.BOOLIsendofchanges =false;//Call SyncFolderItems repeatedly until no more changes is available.//Ssyncstate represents the sync state value is returned in the prior synchronization response. Do{    //Get A list of changes (up to a maximum of 5) that has occurred on normal items in the Inbox folder since the prior s Ynchronization.changecollection<itemchange> ICC = service. SyncFolderItems (NewFolderID (Wellknownfoldername.inbox), Propertyset.firstclassproperties,NULL,5, Syncfolderitemsscope.normalitems, ssyncstate); if(ICC. Count = =0) {Console.WriteLine ("There is no item changes to synchronize."); }    Else    {        foreach(ItemChange ICinchICC) {if(IC. ChangeType = =changetype.create) {//todo:create item on the client.            }            Else if(IC. ChangeType = =changetype.update) {//todo:update item on the client.            }            Else if(IC. ChangeType = =changetype.delete) {//Todo:delete item on the client.            }            Else if(IC. ChangeType = =changetype.readflagchange) {//todo:update The item ' s read flag on the client.} Console.WriteLine ("changetype:"+IC.            Changetype.tostring ()); Console.WriteLine ("ItemId:"+IC.            Itemid.uniqueid); if(IC. Item! =NULL) {Console.WriteLine ("Subject:"+IC.            Item.Subject); } Console.WriteLine ("==========="); }    }    //Save the sync state to use with the future SyncFolderHierarchy calls.Ssyncstate =ICC.    Syncstate; if(!ICC. morechangesavailable) {isendofchanges=true; }}  while(!isendofchanges);
Summarize

Due to the SyncFolderItems method, the returned properties are relatively small, similar to lazy loading, the query efficiency is much higher than finditems, the SyncFolderItems method syncstate string, similar to a pointer, the next synchronization start position. So in the first time when the query is a bit slow, this time can be idonly by the parameters of the Syncfolderitem method, refresh the first state, and use the way in the example in the article, get the status of Count 0, and then the client to save the state on the line, The next time you start synchronizing the server, use this status query and update the status on the client when the results are obtained. For the next use.

Reference

https://msdn.microsoft.com/en-us/library/office/ee693003

Exchange uses EWS managed API2.0 to synchronize mailboxes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.