web
You’re offline. This is a read only version of the page.
close
Skip to main content
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

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

  • Verified answer
    a33ik Profile Picture
    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.

  • Community Member Profile Picture
    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 / Спасибо

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans