Integrated management of IBM i spool files in C# with NTi

Quentin Destrade

Monday 15 July 2024

  • Tutorials

In this article, I present a new method integrated into NTi for listing, filtering and downloading spooled files using the "NTiSpooledFile" object.

IBM i News

Introduction

Navigating the world of IBM i systems, and in particular the management of spool files (or print queue files), can often prove complex, especially when you are looking to integrate them into modern applications. Historically, such integration has required in-depth knowledge of IBM i System APIs, limiting access to a niche of specialist developers.

With NTi, it's now possible to simplify and automate this management directly from a .NET environment in just a few lines.

In this article, I present a new method integrated into NTi for listing, filtering and downloading spool files using the NTiSpooledFile object.

Context

To enable simplified management of spool files with .NET applications, NTi specifically encapsulates three system APIs: QUSCRTUS, QUSLSPL, and QUSRTVUS, providing an abstraction that facilitates their use without requiring deep expertise in IBM i commands.

  • QUSCRTUS : creates a temporary user space to store data.
  • QUSLSPL : lists spool files based on defined criteria.
  • QUSRTVUS : retrieves data from the user space created.

Using NTi to manage spooled files

The ListSpooledFiles() method is designed to facilitate the listing of spooled files according to specific criteria such as output queue, user, form type and user data.

Step 1: Opening the NTi connection

In our application, the DBConnectionService manages the connection to the IBM i, extracting the necessary connection string and initialising NTiConnection.

public class SpoolFilesService
{
    private readonly NTiConnection _conn;

    public SpoolFilesService(DbConnectionService dbConnection)
    {
        _conn = dbConnection.conn;
    }
}

Step 2: Listing Spooled Files

We can now use NTiSpooledFile to query and list the spooled files available. This is done by calling the ListSpooledFiles() method, which filters the files according to the criteria specified by the parameters.

public List<NTiSpooledFile> ListAndFilterSpooledFiles(string filterOutqueue, string filterUser, string filterFormType, string filterUserData)
{
    _conn.Open();
    var spooledFiles = _conn.ListSpooledFiles(filterOutqueue, filterUser, filterFormType, filterUserData);
    _conn.Close();
    return spooledFiles;
}

Step 3: Retrieving and downloading spooled file data

For each spooled file identified, data can be retrieved or downloaded using the GetSpooledFileData() method, which interacts directly with the NTiSpooledFile object.

public byte[] DownloadSpooledFile(NTiSpooledFile file)
{
    try
    {
        _conn.Open();
        return _conn.GetSpooledFileData(file);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Erreur lors de la récupération du fichier : {ex.Message}");
        return null;
    }
    finally
    {
        if (_conn.State == ConnectionState.Open)
        {
            _conn.Close();
        }
    }
}

API Controller

An API controller is used to retrieve and manage spool files, enabling any web interface to list or download spool files with the desired parameters.

For downloading, an object containing metadata is passed as a parameter to identify the file to be downloaded.

[HttpPost("downloadFile")]
public IActionResult DownloadFile([FromBody] NTiSpooledFile file)
{
    try
    {
        var data = _spoolFilesService.DownloadSpooledFile(file);
        if (data == null || data.Length == 0)
        {
            return NotFound("Fichier non trouvé.");
        }

        return File(data, "application/octet-stream", $"{file.FileName}.afp");
    }

    catch (Exception ex)
    {
        return StatusCode(500, $"Erreur interne du serveur: {ex.Message}");
    }
}

Using the ADP Workbench Viewer to view Spoule files

Once our spoule file has been downloaded, to view it efficiently in a simplified way, we use AFP Workbench Viewer for Windows available here.

article 8

Conclusion

With NTi, developing tailor-made solutions for managing spool files on IBM i becomes not only faster but also simpler and more effective. As you can see from our example, in just a few lines we were able to put in place an effective and easily adaptable solution that considerably simplifies spool file management. This approach can be customised to meet the specific needs of your projects, without technical complexity.

To find out more and try out NTi, don't hesitate to contact us.

Quentin Destrade

Back