Friday, July 17, 2009

Create Notes and attach document for Invoice in CRM 4.0 and 3.0

public void CreateNoteAndAddAttachment(string PDFFileNameName , string InvoiceId)
{
service = CLM_CRM_Service.GetCrmService();
//Read File
string currentPath = txtPath.Text;
FileStream stream = File.OpenRead(currentPath + "\\" + PDFFileNameName.ToString() + ".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);
annotation note = new annotation();
note.objectid = new Lookup();
note.objectid.type = EntityName.invoice.ToString();
note.objectid.Value = new Guid(InvoiceId);
note.objecttypecode = new EntityNameReference();
note.objecttypecode.Value = EntityName.invoice.ToString();
note.subject = PDFFileNameName.ToString();
// note.notetext = inviocePDFFileNameName.ToString();
note.documentbody = encodedData;
note.filename = PDFFileNameName.ToString() + ".pdf";
note.mimetype = @"application\pdf";
// Create the note.
Guid newNoteID = service.Create(note);


}

No comments: