Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

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

(0) ShareShare
ReportReport
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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans