A few days ago, I was assigned to a task to download all templates programmatically. As we know, to get the Data Import templates for data migration you have to navigate open CRM UI and navigate to Settings > Data Management > Templates for Data Import. This will open a dialog where you have download templates one a time. It gets really frustrating to download all templates whenever a new design change occurs to the organisation.
The SDK by default has no Messages for downloading the templates, but after playing with Fiddler, I was able to capture the POST Url. Therefore, I can download any template by passing the schema name of the entity in your browser like this:
Replace the ‘account’ with any entity schema name you want I’ll you get the Download dialog like this.
Downloading templates programmatically is so easy as well, all you need is to instantiate the WebClient and pass the proper parameters and call the DownloadFile method.
string fullPath = "account.xml";
string fullUrl = "<
Your
Organization Url>/tools/importwizard/createtemplate.aspx?entityName=account";
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
try
{
webClient.DownloadFile(fullUrl, fullPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
In case you want to download all the templates for all the customizable entities in the organisation:
private void DownloadTemplates()
{
RetrieveAllEntitiesRequest req = new RetrieveAllEntitiesRequest();
req.EntityFilters = EntityFilters.Entity;
req.RetrieveAsIfPublished = true;
RetrieveAllEntitiesResponse resp = (RetrieveAllEntitiesResponse)_sdk.Execute(req);
List<
EntityMetadata
> metadataList = resp.EntityMetadata.ToList();
metadataList.Where(m => m.IsCustomizable.Value == true).ToList().ForEach(entityMetadata =>
{
string fullPath = string.Format("{0}.xml", entityMetadata.SchemaName.ToLower());
string fullUrl =
string.Format("<
Your
Organization Url>/tools/importwizard/createtemplate.aspx?entityName=", entityMetadata.SchemaName.ToLower());
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
try
{
webClient.DownloadFile(fullUrl, fullPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
});
}