Jerome Lefebvre

Localizing the AF Example kits - Part 1 - Exporting a database

Blog Post created by Jerome Lefebvre on Aug 30, 2016

This is an ongoing series introducing the various code examples I and others have created in the goal of localizing the various AF example kits.


Part 0: URI Builder data reference in AF 2016

Part 1: Exporting a database


When working to develop an AF Database and all that this contains, AF Tables, Elements, Templates, categories, UOMs, etc, you frequently need to share your work with others.

PI System Explorer offers many ways to do this. You can reach the export menu from the file menu to export your whole database.

Or you can be more selective and only pick out a single element to export.


And this is possible to do this manually every time you want to export your database to share with colleagues that may not have access to the same AF server as you do. Doing so manually can lead to errors, such as forgetting to include certain objects, such as UOMs. To avoid this, it is also possible to script this using AF SDK and using the PISystem.ExportXML methods. Here is an example that is used to export a database and several UOMS. This is the typical export that is done with the AF example kits.


The simplest example of using the method would be the following. To export the content of a database, the first thing you need to do is grab a reference to that database, in this case, I simply grab the default database. You can then pass this argument directly to the ExportXML. The other arguments are as follows, selecting an export mode: PIExportMode Enumeration. As this database is meant for users, using a different AF server, none of the IDs will match the system that I am using, thus the export mode I select is NoUniqueID. The next one is for convenience as it allows one to specify a path to where I want this export to be saved to.The next two are related to setting a start and end time for exporting event frames and the last an EventHandler to monitor the export process. I did not need the last three arguments, thus they are all set to null.


var db = pisystem.Databases.DefaultDatabase;
pisystem.ExportXml(db, PIExportMode.NoUniqueID, $"{directoryPath}\\OSIDemo_Utilities Management System_{version}.xml", null, null, null);

As further example, frequently you need to export UOMs that are used in the various elements that you have created in a database. To do so, one can simply grab the object, in this case a UOMClass object and use the export method on it. (Note that as there are no ways to build a UOMClasses object, we export each UOMClass to a different file.)


foreach (var UOMClassName in new string[] { "Power", "Energy Cost", "Volume Cost", "US Currency", "Energy per Volume" }) {
    var UOMClass = pisystem.UOMDatabase.UOMClasses[UOMClassName];
    pisystem.ExportXml(UOMClass, PIExportMode.NoUniqueID, $"{directoryPath}\\UOM_{UOMClass.Name}_{version}.xml", null, null, null);


The above code can be found at: AF-Example-Kit-Utilities/ExportKit at master · JeromeLefebvre/AF-Example-Kit-Utilities · GitHub


This work was done with Jason Jian-Hao Lu