web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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.

I have the same question (1)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans