Skip to main content

Hi,

I need to create xml file and then need to upload into drive location.

Below code for creating xml file and download into local machine.

 #region Actions
        public PXAction<POOrder> MyAction;
        ÂPXUIField(DisplayName = "Export XML", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
        ePXButton(CommitChanges = true)]
        public virtual IEnumerable myAction(PXAdapter adapter){
           
          XmlDocument xmlDoc = new XmlDocument();
          XmlDeclaration xmldecl;
          xmldecl=xmlDoc.CreateXmlDeclaration("1.0", null, null);

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

          foreach (InventoryItem dacRecord in dataview.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("ItemType");
                xmlDACField1.AppendChild(xmlDoc.CreateTextNode(dacRecord.ItemType.ToString()));
                xmlDACRecord.AppendChild(xmlDACField1);
               
               XmlNode xmlDACField2 = xmlDoc.CreateElement("Item");
               xmlDACField2.AppendChild(xmlDoc.CreateTextNode(dacRecord.InventoryCD.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(),
                                                          "filename.xml",
                                                          null,
                                                          System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml)),
                                       true);
         return adapter.Get();
             }
 #endregion
 

Can you please help for me to upload this file into drive location using acumatica.

 

Hi @jeewanishalika20 

To upload the file to a drive location using Acumatica, you can refer to the following code.

#region Actions
public PXAction<POOrder> MyAction;
>PXUIField(DisplayName = "Export XML", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
>PXButton(CommitChanges = true)]
public virtual IEnumerable myAction(PXAdapter adapter)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = xmlDoc.CreateXmlDeclaration("1.0", null, null);

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

foreach (InventoryItem dacRecord in dataview.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("ItemType");
xmlDACField1.AppendChild(xmlDoc.CreateTextNode(dacRecord.ItemType.ToString()));
xmlDACRecord.AppendChild(xmlDACField1);

XmlNode xmlDACField2 = xmlDoc.CreateElement("Item");
xmlDACField2.AppendChild(xmlDoc.CreateTextNode(dacRecord.InventoryCD.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);

// Save XML document to a file on the server
string fileName = "filename.xml";
string filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/") + fileName;
xmlDoc.Save(filePath);

// Upload the XML file to the desired drive location
var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
bytee] fileData = System.IO.File.ReadAllBytes(filePath);
PX.SM.FileInfo fileInfo = new PX.SM.FileInfo(Guid.NewGuid(), fileName, null, fileData);
uploadFileMaintenance.SaveFile(fileInfo);

// Delete the temporary XML file from the server
System.IO.File.Delete(filePath);

return adapter.Get();
}
#endregion

In this code, the XML document is saved to a temporary file on the server using the Save method of the XmlDocument class. Then, the UploadFileMaintenance graph is used to read the file from the server, create a PX.SM.FileInfo object, and save the file using the Save File method. After the file is uploaded, the temporary XML file is deleted from the server using System.IO.File.Delete.

 

Make sure to replace "filename.xml" with the desired file name and update the file path in the filePath variable to match the location where you want to temporarily save the file on the server.

Hope it helps.!

Regards,

Sweta


Reply