Click or drag to resize
OSIsoft, LLC

AFSearchFindObjectIds 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:
public abstract IEnumerable<Guid> FindObjectIds(
	int startIndex = 0,
	int pageSize = 0


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.
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.
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.
// 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>();

    // 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);
        // Find the element IDs and load the attributes we will be using.
        var results = search.FindObjectIds(startIndex, pageSize: pageSize);
        int c = 0;
        foreach (Guid id in results)
            if (c >= pageSize)

        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)
                AFAttributeList attributes = new AFAttributeList();
                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);
                    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);
Version Information


Supported in: 2.10.5, 2.10, 2.9.5, 2.9, 2.8.5
See Also
Enabling Operational Intelligence