Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

C# Open XLSX file with OpenXML, add values and save it as an attachment to a note

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hey guys,

once again I have a very specific requirement which is cooking up in my brain.

I have two custom entities.
1: Template Entity X
2: Record Entity Y

Whenever a specific field on Record Entity Y has  a specific value, a record from Template Entitiy X is retrieved.
This Template Entity X record contains a note with an attachment. That Attachment is an XLSX Excel file.

My Plug-In gets that XLSX file from that template entity X record.

Now I want to open that file with OpenXML, write some values in some columns, close the file and add it as a new attachment to a note to the Record Entity Y record which triggered the plugin.

This is the code I got so far:

#region RetrieveTemplateRecord
//Retrieve Template Record
QueryByAttribute template_query = new QueryByAttribute("new_customtemplates");
template_query.AddAttributeValue("new_name", "sample_template");
template_query.ColumnSet = new ColumnSet(true);
EntityCollection template_result = service.RetrieveMultiple(template_query);
Entity template_record = template_result.Entities.First();

//Retrieve Note/Attachment from Template Record
QueryByAttribute template_file_query = new QueryByAttribute("annotation");
template_file_query.AddAttributeValue("objectid", template_record.Id);
template_file_query.ColumnSet = new ColumnSet(true);
EntityCollection template_file_result = service.RetrieveMultiple(template_file_query);
Entity template_file = template_file_result.Entities.First();

//Template Body String
string template_body = template_file.GetAttributeValue("documentbody");
//Template Body Byte Array
byte[] template_data = Convert.FromBase64String(template_body);                
#endregion RetrieveTemplateRecord       

#region OpenTemplateInOpenXML
//CONTINUTE HERE
#endregion OpenTemplateInOpenXML

#region DocumentToPriceList
string entitytype = "new_pricelist";
Entity Note = new Entity("annotation");
Guid EntityToAttachTo = targetEntity.Id; // The GUID of the incident
Note["objectid"] = new EntityReference(entitytype, EntityToAttachTo);
Note["objecttypecode"] = entitytype;
Note["subject"] = document_type;
Note["notetext"] = targetEntity.GetAttributeValue("new_name");
Note["filename"] = template_record.GetAttributeValue("new_name")   ".xlsx";
Note["documentbody"] = Convert.ToBase64String(document_binary);
Note["mimetype"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
service.Create(Note);
#endregion DocumentToPriceList

Can anyone help me out on how to get from the byte[ to a working spreadsheet document in OpenXML and then put it back into a byte[ and then string in order to add it as an attachment to a note?

I have tried several things but cannot find a working way to "open" the document the right way in OpenXML. I Have tried working with a memorystream, with a filestream. But I can't get around the forth and back conversions.

Best wishes and many thankas guys,

Evgeniy

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: C# Open XLSX file with OpenXML, add values and save it as an attachment to a note

    Hey Andrew,

    works perfectly fine!

    Thank you / Спасибо

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: C# Open XLSX file with OpenXML, add values and save it as an attachment to a note

    Hello Evgeniy,

    You can use EPPlus for this purposes - github.com/.../EPPlus

    I haven't tried it in plugins so I don't know would it work in sandbox or not but I tried using it in Azure Functions and it worked fine.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans