AFElement.LoadAttributes Method (IList(AFElement), IList(AFAttributeTemplate))
- Last UpdatedSep 18, 2024
- PI System
- AF SDK 2024
- Developer
Namespace: OSIsoft.AF.Asset
Assembly: OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.1.0.1156
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.Generic.IList<AFElement>
The list of elements to be loaded. If null, then this method does nothing. All elements in the list must be from the same PISystem. - templates
- Type: System.Collections.Generic.IList<AFAttributeTemplate>
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 null), 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 true.
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);
' Get the Database Dim myPISystems As New PISystems() Dim myPISystem As PISystem = myPISystems.DefaultPISystem If myPISystem Is Nothing Then Throw New InvalidOperationException("Default PISystem was not found.") End If Dim myDB As AFDatabase = myPISystem.Databases(dbName) If myDB Is Nothing Then Throw New InvalidOperationException("Database was not found.") End If ' 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"). Dim totalCount As Integer = 0 Using search As 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. Dim tankTemplate As AFElementTemplate = myDB.ElementTemplates("Tank") Dim myAttributes As New AFNamedCollectionList(Of 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. Dim today As New AFTime("T", CultureInfo.CurrentCulture) Dim bbl As UOM = myDB.PISystem.UOMDatabase.UOMs("bbl") Dim meter As UOM = myDB.PISystem.UOMDatabase.UOMs("m") Const pageSize As Integer = 1000 Dim startIndex As Integer = 0 Dim myElements As IList(Of AFElement) = New List(Of AFElement)(pageSize) Do ' Find the elements and load the attributes we will be using. Dim results As IEnumerable(Of AFElement) = search.FindObjects(startIndex, fullLoad:=False, pageSize:=pageSize) myElements.Clear() Dim c As Integer = 0 For Each item As AFElement In results totalCount += 1 myElements.Add(item) c += 1 If c >= pageSize Then Exit For End If Next If myElements.Count = 0 Then Exit Do End If AFElement.LoadAttributes(myElements, myAttributes) ' Once the elements are loaded, we can process them. For Each element As AFElement In myElements Try Dim attributes As New AFAttributeList() attributes.Add(element.Attributes("Volume")) attributes.Add(element.Attributes("Level")) attributes.Add(element.Attributes("Level|HighLimit")) attributes.Add(element.Attributes("Level|LowLimit")) Dim values As AFValues = attributes.GetValue(today) If values(0).IsGood AndAlso values(1).IsGood AndAlso values(2).IsGood AndAlso values(3).IsGood Then Dim volume As Double = CDbl(values(0).Convert(bbl).Value) Dim level As Double = CDbl(values(1).Convert(meter).Value) Dim high As Double = CDbl(values(2).Convert(meter).Value) Dim low As Double = CDbl(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) End If Catch generatedExceptionName As FormatException Console.WriteLine("Error in Tank '{0}'", element.Name) End Try Next ' Advance to next page. startIndex += myElements.Count Loop While myElements.Count > 0 AndAlso myElements.Count >= pageSize End Using Console.WriteLine("Processed {0} Elements.", totalCount)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.