MS Dynamics CRM - Download document template as PDF - C#

Ankaprasad
Posted by Ankaprasad under C# category on | Points: 40 | Views : 1837
The below method will download the document template as PDF and save it to specified folder

 DownloadDocumentTemplatePDF(service, "3c16ef12-f417-ea11-a811-000d3a339ac5", "80bc1f5d-f417-ea11-a811-000d3a339ac5", 10286);


  public static void DownloadDocumentTemplatePDF(IOrganizationService _service, string documentTemplateId, string recordId, int entityTypeCode)
{
string documentTemplateName = "EmployeeDetails";

try
{
OrganizationRequest request = new OrganizationRequest("ExportPdfDocument");

request["EntityTypeCode"] = entityTypeCode;
request["SelectedTemplate"] = new EntityReference("documenttemplate", new Guid(documentTemplateId));

//serialize JSON array of the records
List<Guid> records = new List<Guid> { new Guid(recordId) };
request["SelectedRecords"] = JsonConvert.SerializeObject(records);

OrganizationResponse pdfResponse = (OrganizationResponse)_service.Execute(request);

//Write to file
string b64File = Convert.ToBase64String((byte[])pdfResponse["PdfFile"]);
File.WriteAllBytes(@"C:\Users\M1045533\Desktop\" + documentTemplateName + ".pdf", pdfResponse["PdfFile"] as byte[]);


}
catch (Exception ex)
{
throw;
}
}

Comments or Responses

Login to post response