› Dynamics 365, Enterprise › CRM – Issue with retrieving large document body using Fetch XML

CRM – Issue with retrieving large document body using Fetch XML

 Somesh Siripuram   Leave a comment

Problem Statement:

When we want to export Attachments from CRM (both Notes and Email attachments), we can do this using a Console Application where we read the documentbody from CRM using Fetch XML, and then we convert that to bytes and export to a local folder (or any target system)

This Method is proper and should work properly, which it doss most of the time, but fails on some occasions.

We have observed an issue, where we were not able to open the downloaded document. It would give below error: “There was an error opening the document. The file is damaged and could not be repaired“.

BlogSep92017_1

This was the code that we used:

private static void ExportEmailAttachments()
        {
            string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                        <entity name='activitymimeattachment'>
                            <attribute name='filename' />
                            <attribute name='body' />
                                  <filter type='and'>
                                    <condition attribute='filesize' operator='gt' value='0' />
                                    <condition attribute='filename' operator='not-null' />
                                  
                                <link-entity name='email' from='activityid' to='objectid' alias='ac'>
                                    <attribute name='subject' />
                                    <attribute name='regardingobjectid' />
                                    <attribute name='activity />
                                    <order attribute='subject' descending='false' />
                                    <filter type='and'>
                                        <condition attribute='regardingobjectid' operator='not-null' />
                                        <condition attribute='modifiedon' operator='last-x-months' value='3' />
                                                                                      
                            >";

            string FolderPath = @"D:\Attachments\";
            EntityCollection allEmailAttachments = crmSvc.RetrieveMultiple(new FetchExpression(fetchXML));
            foreach (Entity mimeAttachment in allEmailAttachments.Entities)
            {
                string fileName = mimeAttachment.GetAttributeValue<string>("filename");
                if (fileName.EndsWith("pdf", StringComparison.OrdinalIgnoreCase))
                {
                    string documentBody = mimeAttachment.GetAttributeValue<string>("body");
                    byte[] documentBytes = Convert.FromBase64String(documentBody);

                    File.WriteAllBytes(FolderPath + fileName, documentBytes);
                }
            }
        }

Solution:

We triel lot of methods and various ways to solve, but we found the document was not opening properly. This is what we observed:

  1. The issue does not happen with all the files
  2. It only happens with large files
  3. Observed that the document body from CRM is always exactly 2000 characters

From the above, we got to know that CRM is not sending all the document body in the Fetch XML response.

We were able to solve the above by removing the “distinct=’true’“. We don’t know how it affects the query, but this was the resolution which worked for us. It is strange, but this fixed the issue.

This is the final Fetch XML.

             string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                        <entity name='activitymimeattachment'>
                            <attribute name='filename' />
                            <attribute name='body' />
                                  <filter type='and'>
                                    <condition attribute='filesize' operator='gt' value='0' />
                                    <condition attribute='filename' operator='not-null' />
                                  
                                <link-entity name='email' from='activityid' to='objectid' alias='ac'>
                                    <attribute name='subject' />
                                    <attribute name='regardingobjectid' />
                                    <attribute name='activity />
                                    <order attribute='subject' descending='false' />
                                    <filter type='and'>
                                        <condition attribute='regardingobjectid' operator='not-null' />
                                        <condition attribute='modifiedon' operator='last-x-months' value='3' />
                                    
                                                              
                                ";

Anyone, who can tell why this is the behavior, please feel free to comment below.


Liked it? Share it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.