Lesson 2: Searching for assets
- Last UpdatedSep 29, 2022
- 2 minute read
- PI System
- AF SDK 2.10
- PI Server
After completing this lesson you will be able to:
-
Use AF SDK to search for assets (elements and their attributes).
-
Experiment with the different search criteria that can be used.
-
Evaluate how to handle search results.
Help for methods and data types used in this lesson
Introduction
OSIsoft recommends that you reduce the search results to a minimum on the server, not on the client. In other words, you should call AF SDK methods that allow the server to process the search results instead of writing custom search logic in the client code.
Starting with PI AF 2016 (2.8), search query methods are available that process query strings instead of methods for each type of search. For the exercises in this section, use the new AFSearch methods such as the AFElementSearch class.
For this lesson, imagine you are a software developer for a power company. Your assignment is to develop a client application that allows users to search for electric meters (PI AF Elements) and attributes. When the application is complete, users should be able to search for meters by name, element template, element category, and attribute values. The user should also be able to search for meter attributes by name, element template, and attribute category.
Use the PI AF database named Green Power Company, which is located on your PI AF server. You will be asked to write several methods that search for elements and attributes and print the search results to the console. Before starting, it might help to familiarize yourself with the Green Power Company database using PI System Explorer.
Example
The first example problem is shown here along with its solution. For this first problem, you should implement the following methods inside Program2.cs and run the application with the arguments provided for each of the following methods in .
static void FindMetersByName(AFDatabase database, string elementNameFilter)
{
Console.WriteLine("Find Meters by Name: {0}", elementNameFilter);
// Default search is as an element name string mask.
string querystring = string.Format("{0}", elementNameFilter);
AFElementSearch elementquery = new AFElementSearch(database,
"ElementSearch", querystring);
foreach (AFElement element in elementquery.FindElements())
{
Console.WriteLine("Element: {0}, Template: {1}, Categories: {2}",
element.Name,
element.Template.Name,
element.CategoriesString);
}
Console.WriteLine();