AnsweredAssumed Answered

How get a name for specific Attribute PI AF SDK?

Question asked by JaimeR on Jun 23, 2017
Latest reply on Jun 27, 2017 by JaimeR

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.
        [STAThread]
         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;
                        count++;
                        //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();
                      count2++;
                      if (count2 > 8999)
                          break;
 }
 args2[count2] = "\n";
 if (count2 > 8999)
                        break;
 Console.WriteLine();

 }
                                          
 
                     
            
                              
 if (args2.Length < 2)
 {
 Console.WriteLine("Usage: ConsoleToFile filename output1 output2 output3 ...");
 return;
 }

 //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++)

 {
                        writer.WriteLine(args2[i]);
 }
 }
            
        }
    }
}


















Outcomes