Share via


Upload and manage document templates in Dynamics 365

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Use document templates in Microsoft Dynamics 365 (online & on-premises) to export your Dynamics 365 data as Excel or Word files, which can be used as templates to generate Excel or Word documents with standardized and up-to-date Dynamics 365 data for analysis and reporting purposes. Using document templates ensures consistent and standard data representation for your company and customers. More information:  Help & Training: Work with templates 

After you have created a document template using the web client, you can programmatically upload the template file (.xlsx or . docx) to your Dynamics 365 instance, update the name or the template file associated with a document template record, retrieve the document template record, and delete the document template record. Use the DocumentTemplate entity to upload and manage organization-owned document templates, and the PersonalDocumentTemplate entity to upload and manage user-owned or personal document templates. You can share or assign personal document templates to other users.

To upload a document template, specify the path to the document, the name, the document type (Excel or Word), and the content (file to be uploaded) as a base-64 encoded string. The following code sample demonstrates how to upload an organization-owned Excel template. Before you upload the template, you must have created an Excel template file using the web client.

string filePath = @"C:\ActiveAccounts.xlsx";
DocumentTemplate myTemplate = new DocumentTemplate
{ 
      Name = "Sample Excel Document Template"; 
      DocumentType = new OptionSetValue(1); // For uploading an Excel template. 
      Content = Convert.ToBase64String(File.ReadAllBytes 
         (Path.Combine(Directory.GetCurrentDirectory(), filePath))) 
}; 
_templateID = _serviceProxy.Create(myTemplate); 
Console.WriteLine("Uploaded template: '{0}'.", myTemplate.Name); 

If you want to upload a Word template file instead, specify the path to a Word template file in the filePath variable, and change the DocumentType parameter, as shown in the following example.

DocumentType = new OptionSetValue(2); // For uploading a Word template.

After you upload a template, activate it so it can be used to generate documents. Use the SetStateRequest message to activate the entity instance that you just created.

See Also

DocumentTemplate entity messages and methods
PersonalDocumentTemplate entity messages and methods
Help & Training: Work with templates

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright