Friday, December 19, 2014

How to list all of the files in a folder and sub folder via AX

The following examples will show you how to list all of the files in a folder and sub folder.

 static void GetFilesInFoldersAndSubFolders(Args _args)
{
    System.String[] filePaths = System.IO.Directory::GetFiles(@"folder location", "*.*", System.IO.SearchOption::AllDirectories); //get listing of all files within the folder
    int fileCount = filepaths.get_Length(); //get how many files were found
    int currentFileCount;
   
    //go throw each one of the files that were found
    for(currentFileCount = 0; currentFileCount < fileCount ; ++currentFileCount)
    {
        info(filepaths.GetValue(currentFileCount));
    }
}

Send xml to BarTender via TCP sockets and get response back using UTF-8 encoding

This is an example that can be used to send print jobs/print previews (return jpgs/images via raw text) to the BarTender labeling system or using this example as a foundation to talk to your own servers via tcp sockets

 /// <summary>
/// Send print job btxml string to bartender and get a response back
/// </summary>
/// <param name="defaultXML">
/// the BTXML to send to bartender
/// </param>
/// <returns>
///BarTenderStaus if it was successfull or not
/// </returns>
public BarTenderStaus sendPrintJobTCP(str defaultXML)
{
    str                 response;
    XmlTextReader       xmlTextReader;
    boolean             msgFound;
    System.Text.Encoding encoding;
    str                 errorId;
    boolean             errorsFound;
    str                 message;
    SysOperationProgress previewProgress;
    System.Net.Sockets.TcpClient barTenderClient;
    System.Net.Sockets.NetworkStream stream;
    System.IO.StreamReader reader;
    System.Byte[] send_bytes;
    #avifiles
    try
    {
        //setup progress bar so we can let the user know what we are doing
        previewProgress = SysOperationProgress::newGeneral(#AviPublish2Web, 'Sending Print Job To BarTender', 7);

        if(serverSettings.BarTenderServerName == "" || serverSettings.BarTenderServerPortNumber == 0)
        {
            throw error("No valid BarTender server has been defined. Please contact IT.");
        }
        //update progress bar
        previewProgress.incCount();
        previewProgress.setText("Task: Reading XML...");

        message = defaultXML;
        //check to see if we have something available to send to the server
        if(message == "")
        {
            throw error("No items are available to print");
        }

        //update progress bar
        previewProgress.incCount();
        previewProgress.setText("Task: Connecting to BarTender Server...");
        //make connection to bartender and open stream with UTF8 encoding
        barTenderClient = new System.Net.Sockets.TcpClient(serverSettings.BarTenderServerName, serverSettings.BarTenderServerPortNumber);
        stream = barTenderClient.GetStream();
        reader = new System.IO.StreamReader(stream, System.Text.Encoding::get_UTF8());

        //update progress bar
        previewProgress.incCount();
        previewProgress.setText("Task: Sending Label to BarTender...");
        //define the stream encoding as UTF8
        encoding = System.Text.Encoding::get_UTF8();
        //convert the xml to bytes to send to bartender
        send_bytes = encoding.GetBytes(message);
        //send the xml bytes to bartender
        stream.Write(send_bytes, 0, send_bytes.get_Length());
        //update progress bar
        previewProgress.incCount();
        previewProgress.setText("Task: Reading Response From BarTender...");
        //get the response bartender sends back
        response = reader.ReadToEnd();
        //close the bartender stream
        stream.Close();
        //update progress bar
        previewProgress.incCount();
        previewProgress.setText("Task: Analyzing XML Response from BarTender...");
        //we need to parse the xml response string into xml nodes
        xmlTextReader = XmlTextReader::newXml(response, true);
        //loop through all of the xml nodes
        while(xmlTextReader.read())
        {
            //check to see what type of xml node we are currently on
            switch (xmlTextReader.NodeType())
                {
                    case XmlNodeType::Element:
                        //check to see if we are on the message node
                        if(xmlTextReader.Name() == "Message")
                        {
                            //read the message attributes
                            while (xmlTextReader.MoveToNextAttribute())
                            {
                                //check to see if the attributes node is severity
                                if(xmlTextReader.Name() == "Severity")
                                {
                                    //check to see if we are looking at an error
                                    if(xmlTextReader.Value() == "Error")
                                    {
                                        //error has been found
                                        msgFound = true;
                                        errorsFound = true;
                                    }
                                }
                                else if(xmlTextReader.Name() == "Id")
                                {
                                    //define what the id attribute is incase it is an error
                                    errorId = xmlTextReader.Value();
                                }
                            }
                        }
                        break;
                    case XmlNodeType::Text:
                        //check to see if the last node we were on was a message and if it was an error
                        if(msgFound)
                        {
                             //display error message to user in a friendly manner
                             setPrefix("BarTender Error " + errorId);
                             error(xmlTextReader.Value());
                             //reset message found flag
                             msgFound = false;
                        }
                        break;
                    case XmlNodeType::EndElement: //Display the end of the element.
                        break;
                    default:
                        break;
                }
        }

        //update progress bar
        previewProgress.setText("Task: Finished");
        previewProgress.setTotal(7);
        previewProgress.hide();
        //check to see if any errors occured
        if(!errorsFound)
        {
            return BarTenderStaus::Success;
        }
        else
        {
            return BarTenderStaus::Failure;
        }
    }
    catch
    {
        error("There was an unknown error communicating with the BarTender server. Please restart your computer and try again. If the issue continues please alert IT of this issue.");
        //update progress bar
        previewProgress.setText("Task: Finished");
        previewProgress.setTotal(7);
        previewProgress.hide();
        return BarTenderStaus::Failure;
    }
}