public async Task <ExchangeSyncResult> ExecuteFirstSyncAsync(ExchangeConnectionInfo connectionInfo, ExchangeChangeSet changeSet) { ExchangeSyncResult loginResult = await this.EnsureLoginAsync(this.taskFolderId, connectionInfo); if (loginResult != null) { return(loginResult); } ActiveSyncServer server = CreateExchangeServer(connectionInfo); // Start sync with sync state = 0 // In this case we only obtain a non ero syncKey to use with future queries SyncCommandResult syncCommandResult = await server.Sync(InitialSyncKey, this.taskFolderId, new ExchangeChangeSet()); connectionInfo.PolicyKey = server.PolicyKey; if (syncCommandResult.Status != StatusOk) { var result = new ExchangeSyncResult { AuthorizationResult = this.GetFailedAuthResult("ExecuteFirstSync", syncCommandResult) }; return(result); } // As it is first sync, we have just picked a new SyncId // We have to re-sync with this new SyncId return(await this.ExecuteSyncAsync(connectionInfo, changeSet, syncCommandResult.SyncKey, this.taskFolderId, true)); }
private async Task <ExchangeSyncResult> ExecuteSyncAsync(ExchangeConnectionInfo connectionInfo, ExchangeChangeSet changeSet, string syncState, string folderId, bool isFirstSync) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } if (changeSet == null) { throw new ArgumentNullException("changeSet"); } if (string.IsNullOrEmpty(syncState)) { throw new ArgumentNullException("syncState"); } if (string.IsNullOrEmpty(folderId) || connectionInfo.ServerUri == null || string.IsNullOrWhiteSpace(connectionInfo.ServerUri.ToString())) { ExchangeSyncResult loginResult = await this.EnsureLoginAsync(folderId, connectionInfo); if (loginResult != null) { return(loginResult); } } else { this.taskFolderId = folderId; } ActiveSyncServer server = CreateExchangeServer(connectionInfo); ExchangeSyncResult returnValue = new ExchangeSyncResult { SyncState = syncState, ChangeSet = new ExchangeChangeSet() }; bool mustSync = true; while (mustSync) { SyncCommandResult result = await server.Sync(returnValue.SyncState, this.taskFolderId, changeSet); if (result.Status != StatusOk) { returnValue.AuthorizationResult = this.GetFailedAuthResult("Sync", result); mustSync = false; } else { returnValue.AuthorizationResult = new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.OK, IsOperationSuccess = true, ServerUri = connectionInfo.ServerUri }; if (result.SyncKey != null) { returnValue.SyncState = result.SyncKey; } connectionInfo.PolicyKey = server.PolicyKey; // If we don't have any syncstate (nothing has changed) we return the old one returnValue.OperationResult.IsOperationSuccess = true; returnValue.ChangeSet.AddedTasks.AddRange(result.AddedTasks); returnValue.ChangeSet.ModifiedTasks.AddRange(result.ModifiedTasks); returnValue.ChangeSet.DeletedTasks.AddRange(result.DeletedTasks); returnValue.TaskAddedCount += result.ServerAddedTasks; returnValue.TaskEditedCount += result.ServerModifiedTasks; foreach (var map in result.ClientServerMapIds) { returnValue.AddMap(map.Key, map.Value); } returnValue.TaskDeletedCount += changeSet.DeletedTasks.Count; mustSync = result.MoreAvailable; changeSet = new ExchangeChangeSet(); // changeSet has been pushed to server, reset it ! } } return(returnValue); }