Download Doucument Templates using Console App | CloudFronts

Download 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//

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("", "pass@1234", "");

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.

Share Story :

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.