Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Reference

AFSearch.FindObjectIds Method

  • Last UpdatedNov 18, 2025
  • 6 minute read
AFSearch.FindObjectIds Method
This method will return a list of the ID for each object that matches the search tokens.

Namespace:  OSIsoft.AF.Search
Assembly:  OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.1.1.1182

Syntax

public abstract IEnumerable<Guid> FindObjectIds(
	int startIndex = 0,
	int pageSize = 0
)
Public MustOverride Function FindObjectIds ( 
	Optional startIndex As Integer = 0,
	Optional pageSize As Integer = 0
) As IEnumerable(Of Guid)

Dim instance As AFSearch
Dim startIndex As Integer
Dim pageSize As Integer
Dim returnValue As IEnumerable(Of Guid)

returnValue = instance.FindObjectIds(startIndex, 
	pageSize)
public:
virtual IEnumerable<Guid>^ FindObjectIds(
	int startIndex = 0, 
	int pageSize = 0
) abstract
abstract FindObjectIds : 
        ?startIndex : int * 
        ?pageSize : int 
(* Defaults:
        let _startIndex = defaultArg startIndex 0
        let _pageSize = defaultArg pageSize 0
*)
-> IEnumerable<Guid> 

Parameters

startIndex (Optional)
Type: SystemInt32
The starting index(zero based) of the items to be returned.
pageSize (Optional)
Type: SystemInt32
The page size used for retrieving objects from the server. If this parameter is less than or equal to zero, then the page size will be set to the current value of theCollectionPageSize setting.

Return Value

Type: IEnumerableGuid
Returns a list of the ID for each object that matches the search tokens for this search object.

Exceptions

ExceptionCondition
FormatException This exception is thrown if the ThrowOnError property is and there is a format error in the search query.
NotSupportedException This exception is thrown if the ThrowOnError property is and one of the filters is not supported or the query is too complex to be evaluated by the server.

Remarks

The QuerySearchAnalysis feature can be checked to determine if this search method is supported by the server. If it is not supported by the server, then it will be implemented in the SDK.

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<Guid> myElementIds = new List<Guid>(pageSize);
    do
    {
        // Find the element IDs and load the attributes we will be using.
        var results = search.FindObjectIds(startIndex, pageSize: pageSize);
        myElementIds.Clear();
        int c = 0;
        foreach (Guid id in results)
        {
            totalCount++;
            myElementIds.Add(id);
            c++;
            if (c >= pageSize)
                break;
        }

        if (myElementIds.Count == 0) break;
        AFNamedCollectionList<AFElement> elements =
            AFElement.LoadAttributes(myPISystem, myElementIds, myAttributes);

        // Once the elements are loaded, we can process them.
        foreach (AFElement element in elements)
        {
            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 += myElementIds.Count; // Advance to next page.
    } while (myElementIds.Count > 0 && myElementIds.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 myElementIds As IList(Of Guid) = New List(Of Guid)(pageSize)
    Do
        ' Find the elements and load the attributes we will be using.
        Dim results As IEnumerable(Of Guid) = search.FindObjectIds(startIndex, pageSize:=pageSize)
        myElementIds.Clear()
        Dim c As Integer = 0
        For Each id As Guid In results
            totalCount += 1
            myElementIds.Add(id)
            c += 1
            If c >= pageSize Then
                Exit For
            End If
        Next

        If myElementIds.Count = 0 Then
            Exit Do
        End If
        Dim elements As AFNamedCollectionList(Of AFElement) =
            AFElement.LoadAttributes(myPISystem, myElementIds, myAttributes)

        ' Once the elements are loaded, we can process them.
        For Each element As AFElement In elements
            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 += myElementIds.Count
    Loop While myElementIds.Count > 0 AndAlso myElementIds.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.

Version Information

AFSDK

Supported in: 3.1.1, 3.1.0, 3.0.2, 3.0.1, 3.0.0, 2.10.11, 2.10.5, 2.10.0, 2.10, 2.9.5, 2.9, 2.8.5

See Also

In This Topic
Related Links
TitleResults for “How to create a CRG?”Also Available in