Skip to main content

Command Palette

Search for a command to run...

How to Retrieve Knowledge Article Attachments (msdyn_kbattachment) Using FetchXML

Step-by-step guide to querying and downloading Knowledge Article attachments in Dynamics 365 using FetchXML and the Web API

Updated
2 min read

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:

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:

Example:

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.

<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:

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:

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

For example:

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.

{
"@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:

{
  "@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#:

// 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.