Synchronizing attachments

Complexity: Beginner Data Requirement: ArcGIS Tutorial Data for Desktop

By default, FeatureSyncAgent automatically synchronizes features and their attachments (if enabled). However, you can also choose to synchronize only the FeatureSource without its attachment files by setting FeatureSyncAgent.SynchronizeAttachments property to be false. In either case, attachment information will still be downloaded.

On the other hand, you might use a FeatureAttachmentSyncAgent to synchronize only attachments between local storage and the back-end database. Similar to a FeatureSyncAgent, by default, a FeatureAttachmentSyncAgent's SynchronizationDirection property is Bidirectional. You can set this property to DownloadOnly or UploadOnly to only download/upload attachments respectively.

This topic focuses on downloading attachments To learn more about uploading attachments, read Uploading attachments.

Basic workflow for downloading attachments

Under some circumstances you might want to download attachments without their related features. For example, you already have features existing in cache and you want to get their attachments from the database. You can create a FeatureAttachmentSyncAgent object to help your attachment download task.

The following steps shows only the essential steps to download attachments from a server without getting the features. Before going to this step have the target FeatureSource downloaded.

//Get an access to the FeatureSource as well as the MobileServiceConnection 
FeatureSource featureSource = mobileCache.FeatureSources[0] as FeatureSource;
MobileServiceConnection con = new MobileServiceConnection();
con.Url = @"http://NAMEOFYOURSERVER/ArcGIS/services/NAMEOFYOURSERVICE/MapServer/MobileServer";

//Then create the FeatureAttachmentSyncAgent object and set the sync direction 
FeatureAttachmentSyncAgent featureAttachmentSyncAgent = new FeatureAttachmentSyncAgent(featureSource, con);

//if the SynchronizationDirection property is not set then a full synchronization will occur
featureAttachmentSyncAgent.SynchronizationDirection = SyncDirection.DownloadOnly;

//Then download attachments
featureAttachmentSyncAgent.Synchronize();
NoteNote:

A FeatureAttachmentSyncResults object is returned when the Synchronize operation completes. To learn more about FeatureAttachmentSyncResults, see Understanding attachment sync results.

Downloading attachments using additional options

You can make any of the following additional settings to a FeatureAttachmentSyncAgent object to download only a subset of the attachments, as illustrated by the following workflow.

Steps:
  1. Create a FeatureAttachmentSyncAgent object and set its SynchronizationDirection to be DownloadOnly.
    //similar to the sample at the last section
    MobileServiceConnection con = new MobileServiceConnection();
    con.Url = @"http://NAMEOFYOURSERVER/ArcGIS/services/NAMEOFYOURSERVICE/MapServer/MobileServer";
    FeatureSource featureSource = mobileCache.FeatureSources[0] as FeatureSource;
    
    FeatureAttachmentSyncAgent featureAttachmentSyncAgent = new FeatureAttachmentSyncAgent(featureSource, con);
    featureAttachmentSyncAgent.SynchronizationDirection = SyncDirection.DownloadOnly;
    
  2. Select a subset of features and get their attachment information to download. To do this, use an AttachmentManager object. If AttachmentsToDownload is not set, all attachments of the FeatureSource are downloaded. To learn more about AttachmentManager, read Developing with attachments.
    //use an AttachmentManager to specify we will only download attachments of feature 615
    AttachmentManager manager = featureSource.AttachmentManager;
    IList<Attachment> attachments = 
     manager.GetAttachments(new QueryFilter("OBJECTID = 615"), EditState.Original);
    
    if(attachments.Count > 0)
    {
      //set the attachments to download to the ones retrieved by the AttachmentManager object  
      featureAttachmentSyncAgent.AttachmentsToDownload = attachments;  
    }
    
  3. Select to only download .jpg images with size no bigger than 1306 KB. Finally, you download the attachments. Note that the MaxAttachmentDownloadSize indicates the maximum size (not the size on disk) in bytes of a single attachment object to be downloaded. If the MaxAttachmentDownloadSize is not set or set to 0, attachments of any size download.
    //Set the content type and maximum attachment size to download
    featureAttachmentSyncAgent.DownloadContentType = "image/jpeg";
    featureAttachmentSyncAgent.MaxAttachmentDownloadSize = 1306;
    
    //Then download attachments
    featureAttachmentSyncAgent.Synchronize();
    

    At this point, you have downloaded all .jpg attachment files and each file's size is no larger than 1306 KB for feature 615.

    NoteNote:

    A FeatureAttachmentSyncResults object is returned when the Synchronize operation completes. To learn more about FeatureAttachmentSyncResults, see Understanding attachment sync results.

1/7/2015