Paragon
Paragon

Reputation: 23

Dynamics CRM - Downloading files from notes in C# results in corrupt files with FetchXML

I am using Dynamics CRM and am trying to download a file from the notes (annotation) entity. I am getting the Base64 String and converting to Byte[] like this:

 Convert.FromBase64String((string)item.Attributes["documentbody"]);

But when I do so and try to open the file it is corrupt. Word Documents and PDFs refuse to open. Images however open partially. Ie. I get about 3/4 of the image but the rest of the image is solid grey.

I am guessing FetchXML is not returning the full string. Is there a way to to do this in FetchXML, overiding the limit? I have Googled but cannot see any mention of FetchXML limit?

There is a similar SO question here: FetchXml Query Returns Trimmed DocumentBody?

But I am not using "distinct" in my query.

Here is what I have so far:

<fetch>
  <entity name="annotation" >
    <attribute name="documentbody" />
    <filter type="and" >
      <condition attribute="objectid" operator="eq" value="{D4FFE0CD-CDFA-E711-80E4-005056BA77B6}" />
      <condition attribute="isdocument" operator="eq" value="1" />  
    </filter>
    <order attribute="modifiedon" descending="true" />
  </entity>
</fetch>

Upvotes: 2

Views: 2435

Answers (2)

Vinod Kumar
Vinod Kumar

Reputation: 771

I believe your fetchXML query have the issue. I too faced the same issue but with images in SSRS reports. Images were not loaded properly/got corrupt. I fixed the issue by updating the fetch XML query with distinct = false as in the below statement.

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
.....
</fetch>

Upvotes: 0

James Wood
James Wood

Reputation: 17562

You don't say how you are saving or opening the file, which I suspect is probably the issue.

Re: "I am guessing FetchXML is not returning the full string" - I don't think that is correct, I've not seen this happen.

This is a working example of saving a file to disk.

Exporting Annotation (Note) Attachment

public void ExportDocuments(IOrganizationService service, String filePath)
{
     String fetch = @"<fetch mapping='logical' count='100' version='1.0'>
          <entity name='annotation'>
               <attribute name='filename' />
               <attribute name='documentbody' />
               <attribute name='mimetype' />
          </entity>
     </fetch>";

     foreach (Entity e in service.RetrieveMultiple(new FetchExpression(fetch)))
     {
          if (!String.IsNullOrWhiteSpace(e.Attributes["documentbody"].ToString()))
          {
               byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString());

               File.WriteAllBytes(filePath + e.Attributes["filename"].ToString(), data);
          }
     }
}

Upvotes: 1

Related Questions