Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Custom data entity export to excel using DocuTemplate

Posted on by 15

I'm building a custom application that generates and stores a layout template using the DocuTemplate apis. This process seems to work fine. I'm able to create and store a template for an excel document. The issue I'm having is on spreadsheet generation. The columns I've defined in the template appear fine but I'm not getting any entity data. 

Below is the prototype code I'm using to generate the excel file. What's not clear to me is when/where the data entity is invoked to extract the data. I assume it's during the DocuTemplateRender.renderToStream operation. I feel I may be missing a step or I'm using the wrong APIs.  Any thoughts or guidance would be appreciated.

private boolean tryGenerateReport(CustomContract _contract)
{
boolean success = false;

DocuTemplate template = DocuTemplate::findTemplate(OfficeAppApplicationType::Excel, _contract.parmLayoutName() + filename);

if (template)
{
boolean initialFiltersAreValid = true;
container filterContainer;

DocuFileSaveResult saveResult = DocuFileSave::promptForSaveLocation(template.TemplateID, MyConstants::ExcelExtension, "layout", "journal");

Map filtersToApply = new Map(Types::String, Types::Class);

this.prepareForExport(_contract.parmLayoutName());

// Create header filters.
ExportToExcelFilterTreeBuilder filterBuilder = new ExportToExcelFilterTreeBuilder(tablestr(ReportHeaderEntity));

var filter = filterBuilder.and(
filterBuilder.areEqual(fieldStr(ReportHeaderEntity, Company), curExt()),
filterBuilder.areEqual(fieldStr(ReportHeaderEntity, Name), _contract.parmLayoutName()));

filtersToApply.insert(tablestr(ReportHeaderEntity), filter);

if (initialFiltersAreValid)
{
System.IO.MemoryStream templateStream = new System.IO.MemoryStream();;

try
{
DocuTemplateRender renderer = new DocuTemplateRender();
renderer.renderTemplateToStream(template, filtersToApply, templateStream);
templateStream.Seek(0, System.IO.SeekOrigin::Begin);
success = this.processExport(templateStream);

// Pass the workbook to the user.
DocuFileSave::processSaveResult(templateStream, saveResult);
}
finally
{
if (templateStream != null)
{
templateStream.Dispose();
}
}
}
}

return success;
}

private boolean processExport(System.IO.Stream _stream)
{
boolean success;

str upLoadResult = ExportToExcelStorageHelper::uploadExportStream(_stream, OfficeAppApplicationType::Excel);

return success;
}

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
    RE: Custom data entity export to excel using DocuTemplate

    hi

    It looks like you are on the right track with using the DocuTemplateRender class to generate the report. According to your code, you are passing a map of filters to apply to the renderTemplateToStream method. This is correct and should work assuming that the filters you are passing are valid.

    To ensure that you are passing the correct data entity to the renderTemplateToStream method, you should check the data source of your template in Dynamics 365. Make sure that it is set to the correct entity that contains the data you want to populate the template with.

    If you are still not getting any entity data, you should also check your filters to make sure that they are correctly filtering the data that you want to display in your report. You can test your filters by running them in a query or report to ensure that they are correctly selecting the data that you want to use.

    Additionally, you may want to consider debugging your code by setting breakpoints and stepping through it to see where the issue might be occurring. This can help you identify any potential errors or issues in your code that may be causing the problem.

    I hope this helps! Let me know if you have any further questions.

    DAniele

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans