AFElement.LoadAttributes Method (IList(AFElement), IList(AFAttributeTemplate))
- Last UpdatedJan 12, 2026
- 5 minute read
- PI System
- AF SDK 3.2.0
- Developer
Namespace: OSIsoft.AF.Asset
Assembly: OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.2.0.7
Syntax
public static void LoadAttributes( IList<AFElement> elements, IList<AFAttributeTemplate> templates )
Public Shared Sub LoadAttributes ( elements As IList(Of AFElement), templates As IList(Of AFAttributeTemplate) ) Dim elements As IList(Of AFElement) Dim templates As IList(Of AFAttributeTemplate) AFElement.LoadAttributes(elements, templates)
public: static void LoadAttributes( IList<AFElement^>^ elements, IList<AFAttributeTemplate^>^ templates )
static member LoadAttributes : elements : IList<AFElement> * templates : IList<AFAttributeTemplate> -> unit
Parameters
- elements
- Type: System.Collections.GenericIListAFElement
The list of elements to be loaded. If , then this method does nothing. All elements in the list must be from the same PISystem. - templates
- Type: System.Collections.GenericIListAFAttributeTemplate
The list of attribute templates which define the attributes of the element to be loaded. If a nested attribute template is specified (its Parent property is not ), then its parents will automatically be loaded.
Remarks
This method will ensure that each AFElement specified in the elements list are at least partially loaded with each AFAttribute defined by the AFAttributeTemplate specified in the templates list. Use this method after finding a list of elements using the FindElements(Int32, Boolean, Int32) method and you only need to access a few of the element's attributes. This will reduce the number of round-trips to the server to load the desired information for each element and reduce the amount of memory used for the loaded element.
If this method is called again for the same element, then any new attributes will be loaded and any existing attributes will be updated. An element will not be updated if it has been modified within the current application and its IsDirty property is .
When using a partially loaded element, you will want to avoid making calls that will cause the element to fully load. The Name or ID can be used when looking up an attribute, but avoid looking up by index or using the Count on the attributes collection. If an attribute is configured with a DataReferencePlugIn, then you should ensure that all of the input attributes used by the DataReference are also at least partially loaded.
| This will only load elements and attributes that have been checked into the server. |
Examples
// Get the Database PISystems myPISystems = new PISystems(); PISystem myPISystem = myPISystems.DefaultPISystem; if (myPISystem == null) throw new InvalidOperationException("Default PISystem was not found."); AFDatabase myDB = myPISystem.Databases[dbName]; if (myDB == null) throw new InvalidOperationException("Database was not found."); // Use attribute value query to find a list of tanks that are // In Service (tanks which are out-of-service have their // Status attribute set to "OS"). int totalCount = 0; using (var search = new AFElementSearch(myDB, "FindTanks", @"Template:'TankAdvanced' |Status:<>'OS'")) { search.CacheTimeout = TimeSpan.FromMinutes(10); // Because we are only going to use a few of the attributes for each // of the elements found, we can reduce the load time and memory space // by just loading the attributes we need. Note that if we inadvertently // use attributes other than these, we will incur a load penalty. AFElementTemplate tankTemplate = myDB.ElementTemplates["Tank"]; AFNamedCollectionList<AFAttributeTemplate> myAttributes = new AFNamedCollectionList<AFAttributeTemplate>(); myAttributes.Add(tankTemplate.AttributeTemplates["Volume"]); myAttributes.Add(tankTemplate.AttributeTemplates["Level"]); myAttributes.Add(tankTemplate.AttributeTemplates["Level|HighLimit"]); myAttributes.Add(tankTemplate.AttributeTemplates["Level|LowLimit"]); // Get today's date and UOMs to be used for converting values. AFTime today = new AFTime("T", CultureInfo.CurrentCulture); UOM bbl = myDB.PISystem.UOMDatabase.UOMs["bbl"]; UOM meter = myDB.PISystem.UOMDatabase.UOMs["m"]; const int pageSize = 1000; int startIndex = 0; IList<AFElement> myElements = new List<AFElement>(pageSize); do { // Find the elements and load the attributes we will be using. var results = search.FindObjects(startIndex, fullLoad: false, pageSize: pageSize); myElements.Clear(); int c = 0; foreach (AFElement item in results) { totalCount++; myElements.Add(item); c++; if (c >= pageSize) break; } if (myElements.Count == 0) break; AFElement.LoadAttributes(myElements, myAttributes); // Once the elements are loaded, we can process them. foreach (AFElement element in myElements) { try { AFAttributeList attributes = new AFAttributeList(); attributes.Add(element.Attributes["Volume"]); attributes.Add(element.Attributes["Level"]); attributes.Add(element.Attributes["Level|HighLimit"]); attributes.Add(element.Attributes["Level|LowLimit"]); AFValues values = attributes.GetValue(today); if (values[0].IsGood && values[1].IsGood && values[2].IsGood && values[3].IsGood) { double volume = (double)values[0].Convert(bbl).Value; double level = (double)values[1].Convert(meter).Value; double high = (double)values[2].Convert(meter).Value; double low = (double)values[3].Convert(meter).Value; Console.WriteLine("Tank Inventory for '{0}' is {1} bbl. %Full={2}", element.Name, volume, 100 * (high - low) / level); } else { Console.WriteLine("Bad data in Tank '{0}'", element.Name); } } catch (FormatException) { Console.WriteLine("Error in Tank '{0}'", element.Name); } } startIndex += myElements.Count; // Advance to next page. } while (myElements.Count > 0 && myElements.Count >= pageSize); } Console.WriteLine("Processed {0} Elements.", totalCount);