Creating PDF notes programmatically in CRM 2015

Hi Folks,

This post will help you in creating notes attachment into CRM programmatically.

 

// Open a file and read the contents into a byte array

FileStream stream = File.OpenRead(@"c:\testfile.pdf");

byte[] byteData = new byte[stream.Length];

stream.Read(byteData, 0, byteData.Length);

stream.Close();

 

// Encode the data using base64.

string encodedData = System.Convert.ToBase64String(byteData);

 

 

// Instantiate an Annotation object.

// See the Entity Metadata topic in the SDK documentation to determine

// which attributes must be set for each entity.

Annotation createAnnotation = new Annotation()

{

Subject = "PDF Attachment",

FileName = "File Name",

DocumentBody = encodedData,

MimeType = @"application\pdf"

};

 

// Create the Annotation object.

_annotationId = _serviceProxy.Create(createAnnotation);

 

In the above code snippet you can change the MimeType based on the type of file that want to upload into CRM. For ex., application/msword for Microsoft Word, application/pdf for PDF etc.,

Hope this helps.

 

Thanks,

Vishnu

Comments

  • Anonymous
    June 30, 2015
    Recent Releases and Announcements

    Cumulative Update #1 for SQL Server 2014 SP1

    https://support
  • Anonymous
    August 17, 2016
    Hi I used similar code to attached pdf file but that pdf file is showing error that file is damaged or not properly decoded but instead of @"application\pdf" if I use @"application\msword" or @"text\plain" then it works.string strMessage = "this is a demo"; byte[] byteData = Encoding.ASCII.GetBytes(strMessage); tracingService.Trace("string in bytes " + byteData); string encodedData = Convert.ToBase64String(byteData); tracingService.Trace("Encoded Data " + encodedData); Entity Annotation = new Entity("annotation"); Annotation.Attributes["subject"] = "Demo Note 7"; Annotation.Attributes["documentbody"] = encodedData; Annotation.Attributes["mimetype"] = @"application/pdf"; Annotation.Attributes["notetext"] = "Sample attachment 7"; Annotation.Attributes["filename"] = "Demo7.pdf"; service.Create(Annotation);
    • Anonymous
      September 13, 2016
      Hi Sneha,Surprise for me because if the file is PDF then the mime type should be application\pdf.