Creating attachments

Complexity: Beginner Data Requirement: ArcGIS Tutorial Data for Desktop

Before a new attachment object can be manipulated, you need to add it to the FeatureSource via an AttachmentManager object using the AddAttachment method. Note that attachments can only be added to features that already exist in local storage. If you intend to create attachments and associate it with a new feature, make sure the new feature gets saved before you add attachments to it.

This topic discusses how to add attachment information in the local storage.

Adding attachments to a feature

The following steps show you how to create an attachment object and add it to the FeatureSource using an AttachmentManager object.

Steps:
  1. Create an attachment object by specifying the feature layer and the feature ID to which the attachment belongs. Name the attachment object. Note the attachment name is not the storage path of the attachment file, and attachments added to the same feature can have the same name.
    Attachment attachment = new Attachment(featureSource, 615, "WaterMeter1.jpg");
    
  2. Use AttachmentManager object to add the attachment to the FeatureSource. You can get access to AttachmentManager object through the FeatureSource.AttachmentManager property. To add an attachment, specify the attachment object, its source's storage path, and if you want to copy or move the source file to the cache. To actually upload an attachment and to know more about the available upload options, read Synchronizing attachments.
    AttachmentManager manager = featureSource.AttachmentManager;
    
    //Before you try to add the attachment, you can check if the featureSource enables attachments, 
    //and if so, whether the attachment table have GlobalIds for editing
    if(manager.HasAttachments && manager.AllowEdits)
    {
      //Copy the attachment file from the source location (the SD card) to the cache
      manager.AddAttachment(attachment, @"SD-MMC card\Pictures\WaterMeter2011.jpg", FileOperation.CopyFile);
    }
    

    When done, the actual attachment file is copied/moved to the cache folder.

  3. After adding an attachment object to the FeatureSource, you can view its properties for details. The code block below shows you how to do this.
    //Id: When an attachment is created in local storage and not uploaded yet, 
    //a negative value will be automatically assigned to be its Id.  
    int attachmentId = attachment.Id;
    
    //Size: Size of the attachment object in KB.
    int attachmentSize = attachment.Size;
    
    //FilePath and FileExist: FilePath refers to the location of the attachment in local storage after 
    //it is been added by AttachmentManager, but NOT the source location of attachment file. If this file path 
    //exists then attachment.FileExist will return true.
    string attachmentFilePath = attachment.FilePath;
    

1/7/2015