6 Replies Latest reply on Aug 12, 2016 5:43 PM by karmstrong

    How do I retrieve attributes from multiple elements at a time?

    karmstrong

      This has been a particularly frustrating task - I've read :

      https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/203b8cca-678e-4009-846e-4df75bc65a32.htm

       

      And maybe I'm missing something simple.

       

      I want to read from a PI AF database, find all elements relating to a template, then find the attributes of that template, then find the values of each attribute.

      I'm going to be serializing the elements, their attributes, and their matching values to JSON and transmitting them.

       

      I've stared with this:

                           AFElementTemplate afTemplate = _afDB.ElementTemplates[_tankConfig.TankTemplate];
      
      
                          // Lo
                           AFElementTemplate afTemplate = _afDB.ElementTemplates[_tankConfig.TankTemplate];
      
      
                          // Load all elements based on tank template into a named collection list.
                          AFNamedCollection<AFElement> afElements =
                              AFElement.FindElementsByTemplate(_afDB, null, afTemplate, false, AFSortField.Name, AFSortOrder.Ascending, int.MaxValue);
      
      
                          //AFAttributeList attributes = new AFAttributeList();
                          AFAttributeList attributes = new AFAttributeList();
      
      
                          foreach (AFElement element in afElements)
                          {
                              attributes.Add(element.Attributes);
                              //AFAttributeList attributes = new AFAttributeList();
                          }
                          AFValues values = attributes.GetValue(); // Returns ALL values of EACH AFAttribute in the list.
      ad all elements based on tank template into a named collection list.
                          AFNamedCollection<AFElement> afElements =
      
      

       

      Now I know that I need to populate the attributes AFAttributeList with attributes, but I don't know how to do this without using a for loop to add each attribute from each element individually. Furthermore, I can only initialize an AttributeList with an element. Do I need a different attribute list for every case, or should I be using another data structure to hold all of this information?

       

      What is the most conventional means of doing this?

        • Re: How do I retrieve attributes from multiple elements at a time?
          jhim

          Hi, Kalil

           

          If I've understood your question correctly, you want to eliminate the loop which assigns each attribute to a attribute list corresponding to an element, (please let me know if I'm wrong) you can try searching All attributes which belong to a specified Element Template.

          AFAttributeList myAttributes = AFAttribute.FindElementAttributes(_afDB, null, "*", null, _tankConfig.TankTemplate, AFElementType.Any, "*", null, TypeCode.Empty, true, AFSortField.Name, AFSortOrder.Ascending, int.MaxValue);
          

           

          Instead of searching all elements binding on one element template and assigning each of the attributes of each element on an attibutelist, you can search all attributes and bind them on one AFAttributeList.

          In order to check which attribute belongs to which element, you can call the AFAttribute.Element property.

          Ultimately, if you want to process each attributes one at a time, you will need to use a loop. However, you'll be able to avoid using a loop just to assign each attribute of an element to an attributelist.

          1 of 1 people found this helpful