Download Doucument Templates using Console App - CloudFronts

Download Doucument Templates using Console App

Posted On June 8, 2018 by Admin Posted in  Tagged in ,

Dynamic 365 development services has a team of experts, D365 architects and developers who works closely in every step in the business while closely understanding the requirements and designing the right solution for the business according to the needs as far as development is concerned the development team has a set of experts and developers that coordinate closely with the client and by using standard microsoft development technologies like visual studio and TFS online a robust and concrete development process is strategised. After developing the application it is internally tested according to the business needs and submitted to microsoft in order to clarify any issues. After that the application is listed on the App Source.

Introduction:

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.

Implementation:

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.

Code:

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("[email protected]", "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";

GetDocumentTemplateContent(documentTemplateId);
Console.ReadKey();
}

}

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

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

_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;

try
{
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)
{
throw;
}

}

}
}

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.

Conclusion:

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 :

Secured By miniOrange