# How to Retrieve Knowledge Article Attachments (msdyn_kbattachment) Using FetchXML

In Dynamics 365, knowledge articles often come with file attachments (such as PDFs, images, or documents) stored in the **msdyn\_kbattachment** entity. If you want to retrieve these attachments programmatically, you can use **FetchXML** to query the related records and then make an HTTP request to download the attachment in base64 format.

This blog will walk you through the process step by step.

Lets say you have a knowledge article with 3 attachments:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1758887476311/4357b197-7838-4bf4-addd-b9d8cd87d639.png align="center")

## Step 1: Get the Knowledge Article ID

Before you can retrieve attachments, you need the **Knowledge Article ID** (`knowledgearticleid`) of the article that has attachments. This ID will be passed into the FetchXML query.

You can extract the knowledge article id from the URL:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1758887533226/0bc3f307-5d57-4670-a1ae-769cf8f3ad7b.png align="center")

Example:

```plaintext
86284168-ac72-4f7d-ae1b-7e1e68b0c8ca
```

## Step 2: Build the FetchXML Query

Use the following FetchXML to retrieve attachments for a given knowledge article. Replace the `knowledgearticleid` value with your own.

```xml
<fetch version="1.0" output-format="xml-platform" mapping="logical" returntotalrecordcount="true" no-lock="false">
  <entity name="msdyn_kbattachment">
    <attribute name="msdyn_fileicon_url" />
    <attribute name="statecode" />
    <attribute name="msdyn_filename" />
    <attribute name="msdyn_filesize" />
    <attribute name="msdyn_kbattachmentid" />
    <attribute name="msdyn_filetype" />
    <order attribute="msdyn_filename" descending="false" />
    <link-entity name="msdyn_msdyn_kbattachment_knowledgearticle" intersect="true" visible="false" to="msdyn_kbattachmentid" from="msdyn_kbattachmentid">
      <link-entity name="knowledgearticle" from="knowledgearticleid" to="knowledgearticleid" alias="ka">
        <filter type="and">
          <condition attribute="knowledgearticleid" operator="eq" uitype="knowledgearticle" value="86284168-ac72-4f7d-ae1b-7e1e68b0c8ca" />
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch>
```

This query will return the following details for each attachment:

* **File Name** (`msdyn_filename`)
    
* **File Size** (`msdyn_filesize`)
    
* **File Type** (`msdyn_filetype`)
    
* **Attachment ID** (`msdyn_kbattachmentid`)
    
* **File Icon URL** (`msdyn_fileicon_url`)
    
* **State Code** (`statecode`)
    

Response:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1758887619437/7edba861-cd44-434a-a239-d21e11f6e3b1.png align="center")

## Step 3: Download the Attachment Content

Once you have the `msdyn_kbattachmentid` from the FetchXML results, you can retrieve the actual file content using a Web API HTTP GET request.

Example request:

```http
GET https://<your_org>.api.crm4.dynamics.com/api/data/v9.2/msdyn_kbattachments(<msdyn_kbattachmentid>)/msdyn_fileattachment
```

For example:

```http
GET https://yourorg.api.crm4.dynamics.com/api/data/v9.2/msdyn_kbattachments(2b76e2ec-ce9a-f011-b4cc-000d3adead23)/msdyn_fileattachment
```

This will return a response a **JSON** with the **base64-encoded file content**.

```json
{
"@odata.context": "https://yourorg.api.crm4.dynamics.com/api/data/v9.2/$metadata#msdyn_kbattachments(2b76e2ec-ce9a-f011-b4cc-000d3adead23)/msdyn_fileattachment",
"value": "JVBERi0xLjQKJeLjz9MKNCAwIG9iago8PC9UeXBlL..."
}
```

## Step 4: Parse the JSON and Decode the Base64 Content

The HTTP GET response will return JSON with the `value` property containing the base64-encoded file.

Example:

```json
{
  "@odata.context": "https://<org>.api.crm4.dynamics.com/api/data/v9.2/$metadata#msdyn_kbattachments/msdyn_fileattachment",
  "value": "JVBERi0xLjQKJeLjz9MKNCAwIG9iago8PC9UeXBlL..."
}
```

To save the file locally in C#:

```json
// Extract base64 string from JSON response
string base64FileContent = jsonResponse["value"].ToString();

// Decode and save
byte[] fileBytes = Convert.FromBase64String(base64FileContent);
File.WriteAllBytes(@"C:\\Downloads\\Attachment.pdf", fileBytes);
```

---

## Summary

* Use **FetchXML** to retrieve attachment metadata related to a knowledge article.
    
* Get the **Attachment ID** (`msdyn_kbattachmentid`).
    
* Call the **Web API GET endpoint** `/msdyn_fileattachment` to fetch the base64 content.
    
* Parse the JSON and Decode the base64 string and save the file locally.
    

This approach helps you programmatically fetch and download knowledge article attachments stored in Dynamics 365.
