Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Exchange Web Services .NET
>
Tutorial
> Synchronize folders
The following example shows you how to synchronize folders.
C# example
using System; using System.Net; using System.Collections.Generic; using Independentsoft.Exchange; namespace Sample { class Program { static void Main(string[] args) { NetworkCredential credential = new NetworkCredential("username", "password"); Service service = new Service("https://myserver/ews/Exchange.asmx", credential); try { //Initial synscronization. It will recursively retrieve all existing folders from the mailbox SyncFoldersResponse response = service.SyncFolders(StandardFolder.MailboxRoot); //Keep synchronization state in order to use it in next synchronizations string state = response.State; IList
allExistingFolders = response.CreatedFolders; //Create new folder FolderId folderId = service.CreateFolder("test", StandardFolder.MailboxRoot); //Synchronize folders again but now include the state parameter in order to find changes from last synchronization SyncFoldersResponse newResponse = service.SyncFolders(StandardFolder.MailboxRoot, state); IList
newFolders = newResponse.CreatedFolders; IList
deletedFolders = newResponse.DeletedFolders; IList
updatedFolders = newResponse.UpdatedFolders; //Display new created folders for (int i = 0; i < newFolders.Count; i++) { Console.WriteLine(newFolders[i].DisplayName); Console.WriteLine(newFolders[i].FolderId); } Console.Read(); } catch (ServiceRequestException ex) { Console.WriteLine("Error: " + ex.Message); Console.WriteLine("Error: " + ex.XmlMessage); Console.Read(); } catch (WebException ex) { Console.WriteLine("Error: " + ex.Message); Console.Read(); } } } }
VB example
Imports System Imports System.Net Imports System.Collections.Generic Imports Independentsoft.Exchange Namespace Sample Class Module1 Shared Sub Main(ByVal args As String()) Dim credential As New NetworkCredential("username", "parameter") Dim service As New Service("https://myserver/ews/Exchange.asmx", credential) Try 'Initial synscronization. It will recursively retrieve all existing folders from the mailbox Dim response As SyncFoldersResponse = service.SyncFolders(StandardFolder.MailboxRoot) 'Keep synchronization state in order to use it in next synchronizations Dim state As String = response.State Dim allExistingFolders As IList(Of Folder) = response.CreatedFolders 'Create new folder Dim folderId As FolderId = service.CreateFolder("test", StandardFolder.MailboxRoot) 'Synchronize folders again but now include the state parameter in order to find changes from last synchronization Dim newResponse As SyncFoldersResponse = service.SyncFolders(StandardFolder.MailboxRoot, state) Dim newFolders As IList(Of Folder) = newResponse.CreatedFolders Dim deletedFolders As IList(Of FolderId) = newResponse.DeletedFolders Dim updatedFolders As IList(Of Folder) = newResponse.UpdatedFolders 'Display new created folders For i As Integer = 0 To newFolders.Count - 1 Console.WriteLine(newFolders(i).DisplayName) Console.WriteLine(newFolders(i).FolderId) Next Console.Read() Catch ex As ServiceRequestException Console.WriteLine("Error: " + ex.Message) Console.WriteLine("Error: " + ex.XmlMessage) Console.Read() Catch ex As WebException Console.WriteLine("Error: " + ex.Message) Console.Read() End Try End Sub End Class End Namespace
Need help? Ask our developers:
Name*
Email*
Message*