3 Replies Latest reply on Jul 5, 2016 1:20 PM by Marcos Vainer Loeff

    AF SDK Get All Attribute Values for All Elements in a Specified TimeRange


      Hi ,


      I am new to OSI Soft and AF SDK .


      I used PIIntegrator to connect to a Database and read all attributes for all elements of a specific Time . Created a Asset View for a Specific Template and pushed all attribute values to SQL  using PIIntegrator .


      I am working on the same using AF SDK but not able to find out the exact API's to call


      The Scenario I am trying to do is :

      1) Connect to a PI Server

      2) Connect to  a PI Database

      3) Find All Elements of a Specific Element Template

      4) Get Values of all the attributes for all the elements between a time range.


      I am able to achieve Steps 1 , 2 and 3 . I am using AF SDK .NET 4.6 . I came to know from community that I can use AFListData , PIPoint to get the values of all attributes . But these methods / classes are available only in .Net 4 .


      Could you guide me / provide me a sample example using AF SDK 2.80.7444 and .NET 4.6 to retrieve all attribute values for a specific element template elements between a time range .


      Thanks for your help ,

      Please let us know if you require more information.

        • Re: AF SDK Get All Attribute Values for All Elements in a Specified TimeRange

          Hello Gavin,


          Please find below example as one possibility to do what you've been asking for. Whenever possible, please use bulk calls like in this example to reduce roundtrips to the server.

          Please also see Barry's post about Why you should use the new AF SDK Search because it describes how you can create even more efficient code using search methods introduced with AF SDK 2.8


          using System;
          using System.Collections.Generic;
          using OSIsoft.AF;
          using OSIsoft.AF.Asset;
          using OSIsoft.AF.Time;
          using OSIsoft.AF.Data;
          using OSIsoft.AF.PI;
          namespace PIS_13830
              class Program
                  static void Main(string[] args)
                      // Modifying console width and buffer height
                      Console.WindowWidth = 120;
                      Console.BufferHeight = 1000;
                      // PI System (AF Server) connection
                      PISystem afServer = new PISystems()["AFServer"];
                      // Select the database
                      AFDatabase afDB = afServer.Databases["Nu Green"];
                      // Load the Element Template
                      AFElementTemplate afTemplate = afDB.ElementTemplates["Process"];
                      // Load all Elements based on the Template into a named collection list
                      AFNamedCollectionList<AFElement> myElements = AFElement.FindElementsByTemplate(afDB, null, afTemplate, false, AFSortField.Name, AFSortOrder.Ascending, int.MaxValue);
                      // Create an empty Attribute List
                      AFAttributeList attributes = new AFAttributeList();
                      // Iterate through Elements and add their attributes to the Attribute List
                      foreach (AFElement element in myElements)
                      // Define the timerange 
                      AFTime Omega = AFTime.NowInWholeSeconds;
                      AFTime Alpha = Omega.LocalTime.AddHours(-1);
                      AFTimeRange timeRange = new AFTimeRange(Alpha, Omega);
                      // Define the page size
                      PIPagingConfiguration pageConfig = new PIPagingConfiguration(PIPageType.EventCount, 1000);
                      // Bulk call to load data for all attributes within the specified time range; pageConfig applies
                      IEnumerable<AFValues> values = attributes.Data.RecordedValues(timeRange, AFBoundaryType.Inside, "", false, pageConfig, int.MaxValue);
                      // The returned object is an IEnumerable<AFValues> whith one AFValues object for each AFAttribute
                      foreach (AFValues attributeValues in values)
                          if (attributeValues.Count > 0)
                              Console.WriteLine("Attribute: {0} .. values .. ", attributeValues.Attribute.GetPath());
                              foreach (AFValue value in attributeValues)
                                  Console.WriteLine("  {1} at {2} ", value.Attribute.GetPath(), value.Value, value.Timestamp);
                              Console.WriteLine("No values for Attribute {0}", attributeValues.Attribute.GetPath());
                      Console.Write("Done. Press any key to quit .. ");
          2 of 2 people found this helpful