ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.Data Namespace / Attachment Class
Members Example

In This Topic
    Attachment Class
    In This Topic
    Represents a document associated with a row.
    Syntax
    Public NotInheritable Class Attachment 
       Inherits ArcGIS.Core.CoreObjectsBase
       Implements System.IDisposable 
    public sealed class Attachment : ArcGIS.Core.CoreObjectsBase, System.IDisposable  
    Example
    Adding Attachments
    {
      await QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
        using (FeatureClass parkFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.Park"))
        {
          QueryFilter filter = new QueryFilter { WhereClause = "NUMPARKING > 0" };
    
          using (RowCursor parkingCursor = parkFeatureClass.Search(filter, false))
          {
            while (parkingCursor.MoveNext())
            {
              using (MemoryStream stream = CreateMemoryStreamFromContentsOf("Sample.xml"))
              {
                Attachment attachment = new Attachment("Sample.xml", "text/xml", stream);
    
                using (Row row = parkingCursor.Current)
                {
                  long attachmentId = row.AddAttachment(attachment);
                }
              }
            }
          }
        }
      });
      // Helper method to read a file into a memory stream
      MemoryStream CreateMemoryStreamFromContentsOf(String fileNameWithPath)
      {
        MemoryStream memoryStream = new MemoryStream();
    
        using (FileStream file = new FileStream(fileNameWithPath, FileMode.Open, FileAccess.Read))
        {
          byte[] bytes = new byte[file.Length];
          file.Read(bytes, 0, (int)file.Length);
          memoryStream.Write(bytes, 0, (int)file.Length);
        }
    
        return memoryStream;
      }
    }
    Updating Attachments
    {
      await QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
        using (FeatureClass landUseCaseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.LandUseCase"))
        {
          QueryFilter filter = new QueryFilter { WhereClause = "CASETYPE = 'Rezoning'" };
    
          using (RowCursor landUseCursor = landUseCaseFeatureClass.Search(filter, false))
          {
            while (landUseCursor.MoveNext())
            {
              using (Feature rezoningUseCase = (Feature)landUseCursor.Current)
              {
                IReadOnlyList<Attachment> rezoningAttachments = rezoningUseCase.GetAttachments();
                IEnumerable<Attachment> filteredAttachments = rezoningAttachments.Where(attachment => !attachment.GetName().Contains("rezoning"));
    
                foreach (Attachment attachmentToUpdate in filteredAttachments)
                {
                  attachmentToUpdate.SetName(attachmentToUpdate.GetName().Replace(".pdf", "Rezoning.pdf"));
                  rezoningUseCase.UpdateAttachment(attachmentToUpdate);
                }
              }
            }
          }
        }
      });
    }
    Inheritance Hierarchy

    System.Object
       ArcGIS.Core.CoreObjectsBase
          ArcGIS.Core.Data.Attachment

    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also