Search code examples
c#microsoft-sync-frameworkdata-synchronization

How to upload only the new items to the server without updating/deleting the existing items using Microsoft Sync Framework


I have a data-synchronization scenario where I should upload only the new items to the server without updating/deleting the existing ones and after that download everything from the server. I defined two SyncOrchestrators (one for the upload and one for the download) with different scopes.

How can I specify that only the new items, which don't exist on the server, should be uploaded?

Edit: Scenario description

The client should download everything from the server before going offline. While it is offline it can only create new records. After the client is connected to the network, it should upload the new records and after that download everything from the server.


Solution

  • I solved my problem using information from the following article Manipulating the change dataset in Sync Fx

    Here is my code snippet to synchronize only the new records from the client to the server

    ((SqlSyncProvider)syncOrchestrator.LocalProvider).ChangesSelected += SelectOnlyNewRows;
    
    void SelectOnlyNewRows(object sender, DbChangesSelectedEventArgs e)
    {
      foreach (DataTable table in e.Context.DataSet.Tables)
      {
        for (int i = table.Rows.Count - 1; i >= 0; i--)
        {
          DataRow row = table.Rows[i];
    
          // We are only interested in the new rows.
          if (row.RowState != DataRowState.Added)
            table.Rows.Remove(row); // Delete the row so it doesn't get sent.
        }
      }
    }