Send SSRS Report as an Email attachment using x++ - This stream does not support seek operations

Send Email with PO confirmation report with attachment
class PurchPurchaseEmailController extends SrsReportRunController
{
public static PurchPurchaseOrderController construct()
{
return new PurchPurchaseOrderController();
}

public static void main(Args _args)   
{     
    SrsReportRunController formLetterController = PurchPurchaseOrderController::construct();   
    PurchPurchaseOrderController controller;    
    DocuRef docuref, addedRecord;    
    SRSPrintDestinationSettings settings;   
    SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();   
    SysMailerExchange smtp = new SysMailerExchange();   
    str Body, toMail1;      
    Filename fileName;      
    Array arrayFiles;       
    SysInfologEnumerator sysInfologEnumerator;    
    Log errorMessage;     
    System.Byte[] reportBytes = new System.Byte[0]();  
    SRSProxy srsProxy;     
    SRSReportRunService srsReportRunService = new SrsReportRunService();      
    Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;     
    Map reportParametersMap;      
    SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();    
    _args.record(VendPurchOrderJour::findRecId(_args.record().RecId));       
    VendPurchOrderJour vend = VendPurchOrderJour::findRecId(_args.record().RecId);  
    vendTable vendtable; 
       
    select vendtable where vend.OrderAccount == vendtable.AccountNum;    
    info(strFmt('%1', vendTable.email()));    
    controller = formLetterController;      
 
    controller.parmArgs(_args);     
    controller.parmReportName(PrintMgmtDocType::construct(PrintMgmtDocumentType::PurchaseOrderConfirmationRequest).getDefaultReportFormat());     
    controller.parmShowDialog(false);   
    formLetterController.startOperation();  
 
    fileName = 'Receipt.PDF';           
    settings = controller.parmReportContract().parmPrintSettings();   
    settings.printMediumType(SRSPrintMediumType::File);     
    settings.fileName(fileName);    
    settings.fileFormat(SRSReportFileFormat::PDF);          
    controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());    
    controller.parmReportContract().parmReportExecutionInfo(executionInfo);             
    srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());    
    srsReportRunService.preRunReport(controller.parmreportcontract());   
    reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());   
    parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);   
    srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig()); 
    reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),      
            parameterValueArray,            
            settings.fileFormat(),           
            settings.deviceinfo());   
  
    if (reportBytes)       
    {           
        // Create a memory stream from the report bytes
        System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes); 
 
        // Send the file to the user
        File::SendFileToUser(stream, settings.parmFileName()); 

        // Attach the file to the record
        str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);       
        addedRecord = DocumentManagement::attachFile(vend.TableId, vend.RecId, vend.DataAreaId, "File", stream, fileName, fileContentType, vend.OrderAccount + "Receipt.PDF");      
    }      

    // Prepare email
    toMail1 = vendtable.email(); 
    mailer.setSubject('Purch');   
    mailer.setFrom(SysEmailParameters::find().SMTPUserName);             
    mailer.setBody("Nothing");    
    mailer.addTo(toMail1);                
    mailer.addAttachment(DocumentManagement::getAttachmentStream(addedRecord), addedRecord.Name);   

    // Send email
    smtp.sendNonInteractive(mailer.getMessage());   

    // Attach additional document
    select docuref where docuref.RefRecId == vend.RecId    
        && docuref.RefTableId == vend.TableId            
        && docuref.RefCompanyId == curExt();                  
    mailer.addAttachment(DocumentManagement::getAttachmentStream(docuRef),      
        strFmt('%1%2', docuRef.Name, '.'+docuref.fileExtension()));     
}

}
Error Show - This stream does not support seek operations.
Please suggest

First of all, tell us which line of code is throwing the exception.

Also, it would be good to give us some context about you’re trying to achieve by the part of code that’s failing.

By the way, you’ll make thing easier for yourself (and anyone else reading your code) if you structure your code in a better way. For example, use methods to split your code to logical pieces, each with a descriptive name. Also, I see your code is for D365FO (please don’t forget to attach the tag next time), therefore you can declare methods locally, not on the top of the method, which again makes code easier to read and work with (e.g. when you want to move a piece of code to a separate method).

Hi Martin ,
Thanks for your reply
System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);

    // Send the file to the user
    File::SendFileToUser(stream, settings.parmFileName()); 

    // Attach the file to the record
    str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);       
    addedRecord = DocumentManagement::attachFile(vend.TableId, vend.RecId, vend.DataAreaId, "File", stream, fileName, fileContentType, vend.OrderAccount + "Receipt.PDF");      

this line of code showing error

You showed several lines. Do you mean the last one? If so, it must mean that the exception isn’t actually thrown here, it’s in some code called from DocumentManagement::attachFile(). You can, for example, look at the StackTrace property of the exception to find the actual place.

This is line of code show error - DocumentManagement::attachFile()
Error occured - “Unable to create a communication channel with the SharePoint server 'example.sharepoint.com

That’s a different error than what you mentioned the last time. Are you saying that you aren’t getting This stream does not support seek operations anymore and you’re now getting Unable to create a communication channel with the SharePoint server instead? These are completely different things.

Note that even the title of this thread says that it’s about This stream does not support seek operations; it’s confusing if the actual topic doesn’t match the title. If you want to change the topic, let’s change the title too.

If you have a problem with connection to SharePoint, did you test it? If you don’t know how, you’ll find details in F&O documentation: Configure document management.