A requirement that often comes up in Dynamics 365 F&O development is to print a report. Often, to avoid printing the report again, it can be useful to retrieve the report from the print archive.
What is the Print archive?
The Print archive is an archive used to save generated reports to so they can be printed at a later time. This is useful for long running reports, or any reports, that do not need to be printed immediately or that a user may want to re-print later.
Any user with appropriate permissions can go to the print archive at a later time and reprint the saved report. Remember when printing from the archive, selection criteria does not need to be entered and the report does not need to be re-generated – the archive is used to simply print a report that has already be generated.
The following code snippet may be useful to retrieve the stream of a report present in the print archive:
DocPrintJobHeader dpjh;
DocuRef docuRef;
select firstonly dpjh
where dpjh.DocumentId == _custInvoiceJour.InvoiceId
&& dpjh.DocumentDate == _custInvoiceJour.InvoiceDate;
if (dpjh.RecId && dpjh.PrintJobHeaderRecId)
{
select firstonly docuRef
where docuRef.RefRecId == dpjh.PrintJobHeaderRecId &&
docuRef.ActualCompanyId == _custInvoiceJour.DataAreaId;
if (docuRef)
{
System.IO.Stream generatedReportStream = DocumentManagement::getAttachmentStream(docuRef);
if (generatedReportStream != null && generatedReportStream.Length > 0)
{
// Convert BlobReadStream to MemoryStream
System.IO.MemoryStream memoryStreamLoc = new System.IO.MemoryStream();
generatedReportStream.CopyTo(memoryStreamLoc);
memoryStreamLoc.Position = 0; // Reset the position to the beginning of the stream
//now you can manage the stream of the file
//
//Insert here your business logic
//
}
}
}
In the code, the DocPrintJobHeader table (provided in the Docentric tool) was retrieved using the DocumentID and DocumentDate.
Then I retrieved the DocuRef linked to the DocPrintJobHeader record. Subsequently, with the standard method DocumentManagement::getAttachmentStream(docuRef) I retrieved the file stream.