SDK Searching by record type

Today's question of the day, searching by record type...

The imperative approach

Here we use the methods built into the TrimMainObject search to build up the search clauses to search for the record type with Uri 1.

using (Database database = new Database())
{
    database.Id = "H1";
    database.WorkgroupServerName = "local";
    database.Connect();

    TrimMainObjectSearch search 
      = new TrimMainObjectSearch(database, BaseObjectTypes.Record);

    TrimSearchClause typeClause = new TrimSearchClause(database, BaseObjectTypes.Record, SearchClauseIds.RecordType);
    typeClause.SetCriteriaFromObject(new RecordType(database, 1));

    search.AddSearchClause(typeClause);

    foreach (Record record in search)
    {
        Console.WriteLine(record.Title);
    }
}

String searching

I rarely take the approach demonstrated above, in fact I spent 15 minutes trying (and failing) to work up a more complex example.  Instead I use string searching, like this:

using (Database database = new Database())
{
    database.Id = "H1";
    database.WorkgroupServerName = "local";
    database.Connect();

    TrimMainObjectSearch search 
      = new TrimMainObjectSearch(database, BaseObjectTypes.Record);

    search.SetSearchString("recType:1");

    foreach (Record record in search)
    {
        Console.WriteLine(record.Title);
    }
}

Or you can use the square braces to do a sub search, here we search for all records derived from record types with the behaviour of Box or Folder.

using (Database database = new Database())
{
    database.Id = "H1";
    database.WorkgroupServerName = "local";
    database.Connect();


    TrimMainObjectSearch search = new TrimMainObjectSearch(database, BaseObjectTypes.Record);

    search.SetSearchString("type:[behaviour:Box,Folder]");

    foreach (Record record in search)
    {
        Console.WriteLine(record.Title);
    }
}

Composing string searches

If I am stuck composing a string search I usually:

  • open the HPRM client
  • compose my search using the boolean editor, then
  • switch to string mode.

 I can then copy and paste the string search composed for me by HPRM.