Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Unable to attach excel file using document management at a record header.

Posted on by 150

I am using below sample code to attach a excel file as attachment at record header level. 

using OfficeOpenXml;
class RunnableClass1
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
TCMMassPrintingNoticeTable tabb;
str filename = "MyTest.xlsx";
select firstonly tabb where tabb.TransId == "AK-GA-000000004";
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
using (var package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");

ExcelRange cell = worksheet.Cells.get_Item(1,1);
cell.Value = 'Record type';

package.Save();
}
var mime = System.Web.MimeMapping::GetMimeMapping(filename);
//File::SendFileToUser(stream, 'data.xlsx');

DocumentManagement::attachFileToCommon(tabb, "file", stream, fileName, mime, fileName);
}

}

}

Here "TCMMassPrintingNoticeTable" is the header table used in a custom form where I am attaching the excel file. the code creates and attaches a excel file but it is corrupted. Am I missing something or doing something wrong. Please suggest.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,918 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans