4 Sep’17

D365 GeneralCRM – Issue with retrieving large document body using Fetch XML

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.

Written by

Someswara Siripuram

CRM Team Leader

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Reading more then 10K records in D3FOE OData API

    11 July’ 2018

    Introduction: We all know Dynamics 365 Finance and Operations has limitation of 10K records to be fetched at a time usi...

    Read more
  • Paging in D365 Customer Engagement v9.0

    10 July’ 2018

    Introduction: The Xrm.retrieveMultipleRecords method is used to retrieve a collection of records in Dynamics 365 Custom...

    Read more
  • Set up Dynamics 365 connection in Microsoft Social Engagement

    10 July’ 2018

    Introduction: This blog explains how to Set up Dynamics 365 connection in Microsoft Social Engagement. Steps to be follo...

    Read more
  • Voice of the Customer failed to install

    10 July’ 2018

    Introduction: Many people face issues in installing Voice of Customer solution on v9 environment and trying repeatedly ...

    Read more
  • Scribe Insight AX as a Web Service Find Block issue

    10 July’ 2018

    Introduction: If we need to look up for any value from AX then we do it by using a Find Block in Scribe Insight Eg: Basi...

    Read more

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close