• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Blog
  • Power Platform
    • Power Apps
    • Power Automate
    • Power BI
    • Power Virtual Agents
  • Dynamics 365
  • Azure
  • About Me
Joe Gill Logo

Joe Gill

Microsoft MVP - Power Platform Consultant

Home » Power Platform » Dynamics 365 » CRM Document Templates Some Observations

CRM Document Templates Some Observations

19th January 2016 by Joe Gill Leave a Comment

Document templates were added in the 2016 version of Dynamics CRM to allow users to easily generate Word or Excel documents directly from Dynamics CRM. This  Microsoft link explains how to create and upload a Word template and how to generate documents. However there are a number of things you need to be aware of.

There does not seem to be a way of adding templates to a solution so you need figure out a way to move templates  from one environment to another. You could do this manually or write some code using the DocumentTemplate entity in the 2016 SDK.

Also you cannot modify or export a template that you have loaded in CRM so you need to ensure you keep your original template in a safe place in case you ever want to change it. Alternatively write some code to retrieve it and save it out to a file.  Below is a snippet of code that will retrieve all the document templates and writes them to the c:temp folder. It uses the document type to determine the file type to apply. 

      CrmServiceClient conn = new CrmServiceClient(ConfigurationManager.ConnectionStrings[“CRMConnectionString”].ConnectionString);
      IOrganizationService _service = conn.OrganizationServiceProxy;

      QueryExpression qe = new QueryExpression();
      qe.EntityName = “documenttemplate”;
      qe.ColumnSet = new ColumnSet(true);
      EntityCollection retrieved = _service.RetrieveMultiple(qe);

      foreach (var t in retrieved.Entities)
      {
          Console.WriteLine(“account name:” + t[“name”]);
          int doctype = ((OptionSetValue)t[“documenttype”]).Value;
          string fileName = “c:\temp\” + t[“name”] + (doctype == 1 ? “.xlsx” : “.docx”);
          File.WriteAllBytes(fileName, Convert.FromBase64String((string) t[“content”]));
       }

Share This On Social:
  • Tweet
  • CRM Document Templates Some Observations Joe Gill Dynamics 365 Consultant

Filed Under: .NET, Dynamics 365

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Popular Categories

  • Power Platform
    • Power Apps
    • Power Automate
    • Power Virtual Agents
  • Azure
    • Logic Apps
  • Dynamics 365
  • .NET
  • AI
  • SQL

More to See

Power Platform Requests – Base Request Capacity

17th December 2020 By Joe Gill

Power Automate Desktop

Power Automate Desktop – UI Flow

26th November 2020 By Joe Gill

Tweets

Footer

Joe Gill

Microsoft Business Applications MVP – Power Platform, Dynamics 365 and Azure.

An architect with over twenty years experience designing and developing technology solutions. Specializing in the Microsoft technology stack including Power Platform, Dynamics 365 and Azure. Microsoft MVP Profile

Connect on Social

Useful Links

  • Home
  • Blog
  • About Joe Gill
  • Power Platform
  • Dynamics 365
  • Azure

Featured Posts

Power Platform Requests – Base Request Capacity

Power Automate Desktop – UI Flow

MB-600 – Solution Architect – Supportability

AI Builder – Form Processing Layouts

© 2021 · Joe Gill