Document template in CRM 2016 is used to export CRM data as Excel or Word files, which can be used as templates to generate Excel or Word documents with standardized and up-to-date CRM data for analysis and reporting purposes.
Once created a document template using the web client, we can programmatically upload the template file (.xlsx or . docx) to CRM 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. The DocumentTemplate entity is used to upload and manage organization-owned document templates, and the PersonalDocumentTemplate entity is used to upload and manage user-owned or personal document templates. We 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.
C#
string filePath = @”C:\MyContacts.xlsx”;
DocumentTemplate docTemplate = new DocumentTemplate
{
Name = “Contact Excel Document Template”;
// 1 – For uploading an Excel template, 2 – For uploading word template.
DocumentType = new OptionSetValue(1);
Content = Convert.ToBase64String(File.ReadAllBytes
(Path.Combine(Directory.GetCurrentDirectory(), filePath)))
};
TemplateID = _serviceProxy.Create(docTemplate);
Console.WriteLine(“Uploaded template: ‘{0}’.”, docTemplate.Name);
We have to activate it after document template is uploaded, so it can be used to generate documents. We can use the SetStateRequest message to activate the document template.