Add Attachments (Data Management)

License Level:BasicStandardAdvanced

Summary

Adds file attachments to the records of a geodatabase feature class or table. The attachments are stored internally in the geodatabase in a separate attachment table that maintains linkage to the target dataset. Attachments are added to the target dataset using a match table that dictates for each input record (or an attribute group of records) the path to a file to add as an attachment to that record.

Learn more about geodatabase attachments

Learn more about working with the attachments geoprocessing tools

Illustration

Add Attachment tool illustration
Add Attachments illustration

Usage

Syntax

AddAttachments_management (in_dataset, in_join_field, in_match_table, in_match_join_field, in_match_path_field, {in_working_folder})
ParameterExplanationData Type
in_dataset

Geodatabase table or feature class to add attachments to. Attachments are not added directly to this table, but rather to a related attachment table that maintains linkage to the input dataset.

The input dataset must be stored in a version 10.0 or later geodatabase, and the table must have attachments enabled.

Table View
in_join_field

Field from the Input Dataset that has values that match the values in the Match Join Field. Records that have join field values that match between the Input Dataset and the Match Table will have attachments added. This field can be an Object ID field or any other identifying attribute.

Field
in_match_table

Table that identifies which input records will have attachments added and the paths to those attachments.

Table View
in_match_join_field

Field from the match table that indicates which records in the Input Dataset will have specified attachments added. This field can have values that match Input Dataset Object IDs or some other identifying attribute.

Field
in_match_path_field

Field from the match table that contains paths to the attachments to add to Input Dataset records.

Field
in_working_folder
(Optional)

Folder or workspace where attachment files are centralized. By specifying a working folder, the paths in the Match Path Field can be the short names of files relative to the working folder.

For example, if loading attachments with paths like C:\MyPictures\image1.jpg, C:\MyPictures\image2.jpg, set the Working Folder to C:\MyPictures, then paths in the Match Path Field can be the short names such as image1.jpg and image2.jpg, instead of the longer full paths.

Folder

Code Sample

AddAttachments example (Python window)

The following code snippet illustrates how to use the AddAttachments tool in the Python window.

import arcpy
arcpy.AddAttachments_management(r"C:\Data\City.gdb\Parcels", "ParcelID", r"C:\Data\matchtable.csv", "ParcelID","Picture" , r"C:\Pictures")
AddAttachments example (stand-alone Python script)

The following script illustrates how to use the AddAttachments tool in a stand-alone script.

"""
Example: we have a folder of digital photographs of vacant homes; the photos
are named according to the ParcelID of the house in the picture. Let's add
these photos to a parcel feature class as attachments.
"""

import csv
import arcpy
import os
import sys

input = r"C:\Data\City.gdb\Parcels"
inputField = "ParcelID"
matchTable = r"C:\Data\matchtable.csv"
matchField = "ParcelID"
pathField = "Picture" 
picFolder = r"C:\Pictures"

try:
    # create a new Match Table csv file
    writer = csv.writer(open(matchTable, "wb"), delimiter=",")

    # write a header row (the table will have two columns: ParcelID and Picture)
    writer.writerow([matchField, pathField])

    # iterate through each picture in the directory and write a row to the table
    for file in os.listdir(picFolder):
        if str(file).find(".jpg") > -1:
            writer.writerow([str(file).replace(".jpg", ""), file])

    del writer

    # the input feature class must first be GDB attachments enabled
    arcpy.EnableAttachments_management(input)

    # use the match table with the Add Attachments tool
    arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)
except:
    print arcpy.GetMessages(2)

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: No
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
5/7/2015