2 Replies Latest reply on Jun 27, 2017 12:48 PM by JaimeR

    How get a name for specific Attribute PI AF SDK?


      Hello !  I need some help. I want get a specific Attribute from all alements from a Tamplate configured. Here are some thing I've done:


      1. I can get all Elements from a Tamplate  specific.

      2.  I can get the value from all Attribute for each Elements (line 60)

      3. I cant print my search


      My problem is get the specific name and value  Attribute form all Elements. Can you help me?

      Thank you.



      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Windows.Forms;
      using OSIsoft.AF.PI;  
      using OSIsoft.AF; 
      using OSIsoft.AF.Time;  
      using OSIsoft.AF.Asset;
      using OSIsoft.AF.EventFrame;
      namespace ConnectionAppPIAF
              static class Program
              //The main entry point for the application.
               static void Main()
       PISystems piSystems = new PISystems();
      PISystem piSystem = piSystems["AFServer"];
       AFDatabase afDatabase = piSystem.Databases["Colombia"];
       AFElementTemplate elementTemplates = afDatabase.ElementTemplates["BM"];
                        const int pageSize = 1000;
       int startIndex = 0;
       int totalCount;
       int count = 0;
       string[] args = new string[900];
       string[] args2 = new string[90000];
       // Find a collection of elements instantiated from the BM tempplate.
                          // Only the Elements' header information (Name, Description, Template, Type, etc.)
       // are loaded from the AF Server by this call.
       AFNamedCollection<AFElement> elements = AFElement.FindElements(
       database: afDatabase,
       searchRoot: null,
       query: "BM",
       field: AFSearchField.Template,
       searchFullHierarchy: true,
       sortField: AFSortField.Name,
       sortOrder: AFSortOrder.Ascending,
       startIndex: startIndex,
       maxCount: pageSize,
       totalCount: out totalCount);
       Console.WriteLine("Found {0} Elements.", elements.Count);
                          foreach (AFElement item in elements)
                              args[count] = item.Name;
                              //Console.WriteLine(" Element name is {0}.", item.Name);
                                      startIndex += pageSize;
       foreach (AFElement item in elements)
                          Console.WriteLine("Atributos del elemento: " + item.Name);
                          AFAttributeList attributes = new AFAttributeList(item.Attributes);
       AFValues values = attributes.GetValue();
       foreach (AFValue v in values)
                            Console.WriteLine("Valor: "+v.ToString());
                            args2[count2] = "Valor: " + v.ToString();
                            if (count2 > 8999)
       args2[count2] = "\n";
       if (count2 > 8999)
       if (args2.Length < 2)
       Console.WriteLine("Usage: ConsoleToFile filename output1 output2 output3 ...");
       //write each command line argument to the file
       //string destFilename = args[0];
       string path = @"C:\Users\pepitoperez\Documents\Wells.txt";
       using (StreamWriter writer = File.CreateText(path))
       for (int i = 0; i < args2.Length; i++)
        • Re: How get a name for specific Attribute PI AF SDK?
          Rick Davin

          Hi Jaime,


          Take note that in line 63, this


          AFAttributeList attributes = new AFAttributeList(item.Attributes); 


          will cause a full load of the 'item' element because you have referenced item.Attributes.  To find just a given attribute without forcing a full load, you would use AFAttribute.FindAttribute.  Let's say you want an attribute named "Foo".  Then you would call:


          AFAttribute attribute = AFAttribute.FindAttribute("Foo", item); 


          This might return a null if not found.  But it does not cause a full load of the element.  You could also refer to the name directly as in:


          AFAttribute attribute = item["Foo"]; 


          It too would return a null if not found.  However, calling it in this manner will also cause a full load of the element because item["Foo"] is short for item.Attributes["Foo"].


          For efficiency of partial loading, you can investigate AFElement.LoadAttributes.


          As a side note, you set maxCount: pageSize.  The page size and maximum count of expected items are 2 different things.  If you have more than 1000 elements and since you are using AFElement.FindElements, then you will need to write code to process all the elements by asking for additional pages, with a new startIndex for each page.


          Or better yet, if you are using AF 2.8 or better than you really should be using the AFElementSearch object.  Here it has a pageSize and it loops over all your elements without special code.  That is to say it transparently fetches new pages of data for you.

          3 of 3 people found this helpful