Dynamics 365 – Converting the Word Template to a PDF document through a Plugin/Custom-workflow code

Microsoft has released the long-awaited feature of converting a Word Template to a PDF document from version 9.0.1905.2010. If you want this feature to be used only in the plugin, then no need to enable this in the Sales Hub. Dynamics 365 now has an OOB action “ExportPdfDocument” for converting the word template to the PFD document. Just pass the Entity Type Code, Record ID and Document Template ID to the action and this will return the PDF document in the byte[] format. This action can be executed for any entities, including the custom entities. Add the below piece of code in the Plugin/Custom-workflow to generate the PDF document from the Word Template.

Guid wordTemplateId = new Guid("068f4540-081f-434f-b409-a0e7d07c1a11");
Guid entityId = new Guid("b593913b-7172-4624-abec-da20552230f5");

OrganizationRequest exportPdfAction = new OrganizationRequest("ExportPdfDocument");

exportPdfAction["EntityTypeCode"] = 10072;
exportPdfAction["SelectedTemplate"] = new EntityReference("documenttemplate", wordTemplateId);
exportPdfAction["SelectedRecords"] = "[\'{" + entityId + "}\']";

OrganizationResponse convertPdfResponse = (OrganizationResponse)service.Execute(exportPdfAction);

string pdfFile = Convert.ToBase64String((byte[])convertPdfResponse["PdfFile"]);  

Pass the record Id in the array for the input parameter “SelectedRecords” but note it’s impossible to generate the PDF document for multiple records. You should pass only one record ID in the SelectRecords property. If multiple record IDs are passed to this property, the action will generate the PDF document for only one record ID.

3 thoughts on “Dynamics 365 – Converting the Word Template to a PDF document through a Plugin/Custom-workflow code

  1. The above code when tried using custom workflow throwing error:
    The request ExportPdfDocument cannot be invoked from the Sandbox

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s