---
title: Attachments
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

### Getting the attachments of a page

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Iterates over all attachments of the page
    foreach (DocumentAttachment attachment in page.AllAttachments)
    {
        // Perform any action with the attachment object (DocumentAttachment)
    }
}

// To get only unsorted attachments, use the TreeNode.Attachments collection
// To get only page field attachments, use the TreeNode.GroupedAttachments collection

```

[> Back to list of examples](#toc)

### Inserting attachments into page fields

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    DocumentAttachment attachment = null;

    // Prepares the path of the file
    string file = System.Web.HttpContext.Current.Server.MapPath("/FileFolder/file.png");

    // Inserts the attachment into the 'ArticleTeaserImage' field and updates the page
    attachment = DocumentHelper.AddAttachment(page, "ArticleTeaserImage", file);
    page.Update();
}

```

[> Back to list of examples](#toc)

### Inserting grouped attachments into page fields

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    DocumentAttachment attachment = null;

    // Prepares the path of the file
    string file = System.Web.HttpContext.Current.Server.MapPath("/FileFolder/file.png");

    // Generates a new GUID for the attachment
    Guid attachmentGuid = Guid.NewGuid();

    // Prepares the GUID of the grouped attachment page field (of the 'Attachments' data type)
    // To find the GUID of a field:
    // Navigate to the 'Page types' application -> Select the Page type -> 'Fields' tab -> Select the field
    Guid fieldGuid = Guid.Parse("2458d5fd-54f0-4a07-b2cf-a45c38e793db");

    // Inserts the attachment into the field specified by the GUID and updates the page
    attachment = DocumentHelper.AddGroupedAttachment(page, attachmentGuid, fieldGuid, file);
}

```

[> Back to list of examples](#toc)

### Adding unsorted attachments to pages

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Prepares the path of the file
    string file = System.Web.HttpContext.Current.Server.MapPath("/FileFolder/file.png");

    // Adds the file as an attachment of the page
    DocumentHelper.AddUnsortedAttachment(page, Guid.NewGuid(), file);
}

```

[> Back to list of examples](#toc)

### Changing the order of unsorted attachments

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Gets an attachment by file name
    DocumentAttachment attachment = DocumentHelper.GetAttachment(page, "file.png");

    // Moves the attachment down in the list
    DocumentHelper.MoveAttachmentDown(attachment.AttachmentGUID, page);

    // Moves the attachment up in the list
    DocumentHelper.MoveAttachmentUp(attachment.AttachmentGUID, page);
}

```

[> Back to list of examples](#toc)

### Modifying attachment metadata

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Gets an attachment by file name
    DocumentAttachment attachment = DocumentHelper.GetAttachment(page, "file.png");

    // Edits the attachment's metadata (name, title and description)
    attachment.AttachmentName += " - modified";
    attachment.AttachmentTitle = "Attachment title";
    attachment.AttachmentDescription = "Attachment description.";

    // Ensures that the attachment can be updated without supplying its binary data
    attachment.AllowPartialUpdate = true;

    // Saves the modified attachment into the database
    attachment.Update();
}

```

[> Back to list of examples](#toc)

### Deleting attachments

```csharp

// Gets the first page located under /Articles
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/Articles", PathTypeEnum.Children)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Gets an attachment by file name
    DocumentAttachment attachment = DocumentHelper.GetAttachment(page, "file.png");

    // Deletes the attachment
    DocumentHelper.DeleteAttachment(page, attachment.AttachmentGUID);
}

```

[> Back to list of examples](#toc)
