Solved

XML attachment

  • 30 October 2023
  • 3 replies
  • 66 views

Userlevel 1

I need to attached XML file to File. I developed code for generate XML file and download. How is the possible way change that code to attached XML file to Files.

This is my code :

 #region Action

public PXAction<PX.Objects.SO.SOOrder> Attach;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Attach")]
protected virtual IEnumerable attach(PXAdapter adapter)
{

// Use your favorite library to create XML file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));

// Create root node of XML file
XmlNode rootNode = xmlDoc.CreateElement("GridData");

// Iterate rows of the DataView
foreach (SOLine dacRecord in Base.Transactions.Select())
{
// Create an XML Element to represent the DAC row
XmlNode xmlDACRecord = xmlDoc.CreateElement("DACRecord");

// Add desired DAC fields as child XML Elements of the DAC row XML element
XmlNode xmlDACField1 = xmlDoc.CreateElement("DACField1");
xmlDACField1.AppendChild(xmlDoc.CreateTextNode(dacRecord.InventoryID.ToString()));
xmlDACRecord.AppendChild(xmlDACField1);

XmlNode xmlDACField2 = xmlDoc.CreateElement("DACField2");
xmlDACField2.AppendChild(xmlDoc.CreateTextNode(dacRecord.TranDesc.ToString()));
xmlDACRecord.AppendChild(xmlDACField2);

// Adding XML DAC Record to XML root node
rootNode.AppendChild(xmlDACRecord);
}

// Adding XML root node to XML document
xmlDoc.AppendChild(rootNode);

// Redirect browser to XML file created in memory on server
throw new PXRedirectToFileException(new PX.SM.FileInfo(Guid.NewGuid()+
".xml",
null,
System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml)),
true);



return adapter.Get();

 

icon

Best answer by tharinduweerasooriya90 31 October 2023, 03:31

View original

3 replies

Userlevel 7
Badge +4

Hi @tharinduweerasooriya90,

You can use PXNoteAttribute.SetFileNotes to attach a file to the document. 

Following article has an sample implementation you can refer to,

Feel free to post if you have any questions. Good Luck.! 

Userlevel 1

Hi @tharinduweerasooriya90,

You can use PXNoteAttribute.SetFileNotes to attach a file to the document. 

Following article has an sample implementation you can refer to,

Feel free to post if you have any questions. Good Luck.! 

Thanks

Userlevel 1

I change code to attached xml file to file

 #region Action

public PXAction<PX.Objects.SO.SOOrder> Attach;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Attach")]
public IEnumerable attach(PXAdapter adapter)
{

// Use your favorite library to create XML file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));

// Create root node of XML file
XmlNode rootNode = xmlDoc.CreateElement("GridData");

// Iterate rows of the DataView
foreach (SOLine dacRecord in Base.Transactions.Select())
{
// Create an XML Element to represent the DAC row
XmlNode xmlDACRecord = xmlDoc.CreateElement("DACRecord");

// Add desired DAC fields as child XML Elements of the DAC row XML element
XmlNode xmlDACField1 = xmlDoc.CreateElement("DACField1");
xmlDACField1.AppendChild(xmlDoc.CreateTextNode(dacRecord.InventoryID.ToString()));
xmlDACRecord.AppendChild(xmlDACField1);

XmlNode xmlDACField2 = xmlDoc.CreateElement("DACField2");
xmlDACField2.AppendChild(xmlDoc.CreateTextNode(dacRecord.TranDesc.ToString()));
xmlDACRecord.AppendChild(xmlDACField2);

// Adding XML DAC Record to XML root node
rootNode.AppendChild(xmlDACRecord);
}

// Adding XML root node to XML document
xmlDoc.AppendChild(rootNode);

// Generate info about the file
var fileInfo = new PX.SM.FileInfo(Guid.NewGuid()+ ".xml", null, System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml));

var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
uploadFileMaintenance.SaveFile(fileInfo);
PXNoteAttribute.AttachFile(Base.Caches[typeof(SOOrder)], Base.Document.Current, fileInfo);
return adapter.Get();
}
#endregion

 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved