Search code examples
c#sharepointdynamics-crm-2011memorystream

Uploading document body to SharePoint from CRM Notes C#


I am able to upload the document but when I'm viewing/downloading it, there seems to be an error. It says it ran into a problem opening this PDF. Ran into a problem

I have the following code

using (var stream = new System.IO.MemoryStream())
{
    byte[] myByte = System.Text.ASCIIEncoding.Default.GetBytes(documentBody);
    foreach (byte element in myByte)
    {
        stream.WriteByte(element);
    }
    stream.Seek(0, SeekOrigin.Begin);
    var newFile = new FileCreationInformation { Url = fileName, ContentStream = stream, Overwrite = true };

    file = list.RootFolder.Files.Add(newFile);
    file.CheckOut();
    file.CheckIn(string.Empty, CheckinType.MajorCheckIn);
    context.Load(file);
    context.ExecuteQuery();
}

The documentBody is the field documentbody from Annotation (note). Is there something wrong with the stream?


Solution

  • The documentBody is Base64 encoded in CRM, so you many need to decode it first before saving into SharePoint.

    Try this to get the document data.

    byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString());