Setting Operation Result for Outgoing Synchronization Operations

In this article we will describe how to report an operation result to the Engine for outgoing synchronization operations as well as how to prevent items creation in the remote storage.

Setting the In-Sync Status

By default, if your method implementation completes without exception, the Engine will consider the operation to be successful and the file or folder to be successfully created, updated, deleted or moved in the remote storage. The Engine will mark the item as in-sync with remote storage. If exception is thrown, the item is left in the not in-sync state and the Engine will retry synchronization at a later time.

To explicitly set the result of the operation, each method that modifies the remote storage provides the IInSyncResultContext interface passed as a parameter. To set the result of the operation you can set the IInSyncResultContext.SetInSync property.

Files and Folders Creation

On Windows, the platform API provides only a post-event, which means the item is created in file system, after which you will get a call to IFlder.CreateFileAsync() and IFolder.CreateFolderAsync() methods. At this point you can tell the Engine that you do not want to sync this item to the remote storage by setting IInSyncResultContext.SetInSync property to false. You can also delete the item and update Windows Explorer, to reflect the deletion:

public async Task<IFolderMetadata> CreateFolderAsync(
    IFolderMetadata folderMetadata, 
    IOperationContext operationContext, 
    IInSyncResultContext inSyncResultContext, 
    CancellationToken cancellationToken = default)
{
    string newItemPath = Path.Combine(UserFileSystemPath, folderMetadata.Name);

    if (/* Creating folders not allowed */)
    {
        inSyncResultContext.SetInSync = false;
        MessageBox.Show("Creating folders not allowed", "User File System");
        Directory.Delete(newItemPath, true);
        PlaceholderItem.UpdateUI(UserFileSystemPath);        
        return null;
    }
    ...
}