11 Jun’18

D365 GeneralDownload Doucument Templates using Console App


In this blog we will be demonstrate how to download word document templates from D365 Customer Engagement and then import them to another environment. Often there are requirements to move document templates from one environment to another. But currently adding document templates to solutions is not supported.  To overcome this we have created a console app which downloads the word document template.


Step 1: To download Document templates we have created a Console App and it requires the following details:

  • Username
  • Password
  • Organization Service Endpoint Address
  • GUID of the template word template to download

The username and password are the basic details that we use to log in to https//:www.portal.office.com

To get the Organization Service Endpoint Address, navigate to Settings > Customizations > Developer Resources and copy the address as shown in the image below.

Organization Service Endpoint AddressTo get the GUID of the Template Navigate to Settings > Templates > Document templates and open the template you want to download and Click on the Pop Out option at the top right as shown below

Pop Out Record Option

Then from the URL we can get the GUID of the record as shown

GUID of the record

Step 2:  The code to download the document template is shown below. It downloads the Word template and stores it in the location specified in the code. Remember to change this location to the location on your PC.

Note: Replace all the required values in the code according to your organization details.


using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Net;
using System.ServiceModel.Description;
using System.Text;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System.IO;

namespace DownloadDocumentTemplates
public class DocumentTemplateDownload
static IOrganizationService _service = null;
static OrganizationServiceProxy _proxy = null;
static void Main(string[] args)

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

ConnectToCRM("test@test.onmicrosoft.com", "pass@1234", "https://test.api.crm8.dynamics.com/XRMServices/2011/Organization.svc");

Guid userId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
if (userId != null)
Console.WriteLine("Guid: " + userId);

//GUID of the document template
String documentTemplateId = "094EEB2A-948C-E711-8112-70106FAA45E1";



public static void ConnectToCRM(string _username, string _Password, string _OrgSOAPServiceUri)
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = _username;
credentials.UserName.Password = _Password;

Uri serviceUri = new Uri(_OrgSOAPServiceUri);
_proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);

_service = (IOrganizationService)_proxy;

catch (Exception e)
Console.WriteLine("Error while Connecting: " + e.Message);


public static void GetDocumentTemplateContent(string documentTemplateId)
EntityCollection doc = null;
string content = null;
string documentTemplateName = string.Empty;
Encoding encoding = Encoding.UTF8;

string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='documenttemplate'>
<attribute name='content' />
<attribute name='documenttype' />
<attribute name='name' />
<attribute name='status' />
<attribute name='modifiedon' />
<attribute name='modifiedby' />
<attribute name='description' />
<attribute name='languagecode' />
<attribute name='associatedentitytypecode' />
<order attribute='documenttype' descending='false' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='documenttemplateid' operator='eq' uitype='documenttemplate' value='" + documentTemplateId + @"' />
</filter >
</entity >
</fetch > ";

doc = _service.RetrieveMultiple(new FetchExpression(fetchXML));

if (doc != null)
if (doc.Entities.Count > 0)
content = doc[0].Attributes["content"].ToString();
documentTemplateName = doc[0].Attributes["name"].ToString();

byte[] textAsBytes = Convert.FromBase64String(content);
File.WriteAllBytes(@"C:\Users\test\Desktop\" + documentTemplateName + ".docx", textAsBytes);


catch (Exception)



Step 3:

When the code is run it downloads the document template on the your PC and the document template is named after the template in CRM.


This is very helpful as it can be used in another environment by simply uploading the template. To import the word template we can navigate to Settings > Templates >Document Templates and then upload the template.

Written by

Clinton D'Mello

Trainee Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • “$ undefined error” conflict issue in Dynamics 365 v9.0

    18 June’ 2018

    Introduction: After upgrade of dynamics CRM 8.2 to 9.0 jQuery start breaking or you will receive $ undefined error. Th...

    Read more
  • Creating Custom Fields

    18 June’ 2018

    Introduction: This blog articles explains the ability to add custom fields is available in platform update 13 and later...

    Read more
  • Explore Business Central API through Postman using Basic Authentication

    14 June’ 2018

    Introduction: In this article, we will walk through steps on how to authenticate Business Central API using Basic Authe...

    Read more
  • Setup Positive Pay Export

    14 June’ 2018

    Introduction: What is Positive Pay Export and Why it’s used? Positive Pay Export is a file that is given to the ...

    Read more
  • Create Approval Flow with D365 conection

    12 June’ 2018

    Introduction: This blog explains how to Create Approval Flow using Microsoft Flows.  Use Case: When work order is upd...

    Read more