Get total number of documents within a container in CM23.4

Hi,

I have a bit of strange situation in getting total number of documents within a container using SearchClause with CM SDK. Not sure if it is due to upgrade to CM23.4. I use TrimMainObjectSearch and pass in the search field and value. And from the response, i use 'recContents' field and loop through to get the total number of files. I noticed after the upgrade, if the total file count is less than 200, it display as it is. But if it is more than 200 files in the container, it simply shows "recContents":"This folder contains 343 documents.". Hence it breaks our logic to get the total file count. Is there any way, we can just get the total number of files within a container?

  • Hi Mark,

    Since you have your TrimMainObjectSearch, once you've set your clauses or your SearchString property you can just call 'Count' (not FastCount or VeryFastCount).

    There are rare occurrences where this number may not match either due to the container being actively added to or some type of post result processing although Count should hopefully be pretty close for very large search results (for 343 documents, I'd like to think that Count would be 'spot on' as far as total items is concerned).

    If you are after an equivalent of 'loop through' but not the Count method, the next best is GetResultAsUriArray off the TrimMainObjectSearch.

    This will get you the actual URI's of the items but not have any of the extra properties returned (which is the expensive bit), so calling GetResultAsUriArray and then checking the array length will then be the next best way to go.

    -Scotty

  • Hi Scotty,

    Thanks for the information. But if I call count property, I think the count returns including the container itself. E.g if there are 343 documents,it returns 344. Is it just safe to count-1 to get the actual file count inside the container? Or is it better to loop through and exclude if the record is not the container?

    Mark

  • Hi Mark,

    Are you able to share your search clause part of the code with respect to the TrimMainObjectSearch?

    If you are just doing a 'container:123' search clause then the count should not include the container.

    If you can share the TrimMainObjectSearch part of the code, I'll happily review and recommend the best way forward including some sample code if the Count property is still returning incorrectly.

    -Scotty

  • Hi Mark

    The below example returns the correct count on 23.4.

    Database database = new Database();
    database.Connect();
    TrimMainObjectSearch trimMainObjectSearch = new TrimMainObjectSearch(database, BaseObjectTypes.Record);
    trimMainObjectSearch.SearchString = "container:1234";
    Console.WriteLine(trimMainObjectSearch.Count);

  • Thanks Scotty,

    In search clause, i am  not using container. The requirement I have is to use another additional field (engagement number). That field is defined in the container level and it is a unique field for each container. The code I used is exactly the same as above that you provided except in search string I used "engagementnumber:1234"

    Mark 

  • You could do your initial search to get container uri based on engagementnumber:

    engagementnumber:1234 and type:containerRecTypeUri

    and then do container search based on Container Uri to get contained records

    TrimMainObjectSearch trimMainObjectSearch = new TrimMainObjectSearch(database, BaseObjectTypes.Record);
    trimMainObjectSearch.SearchString = "container:1234";
    Console.WriteLine(trimMainObjectSearch.Count);

  • Verified Answer

    Can also combine those two steps into one Slight smile

    trimMainObjectSearch = new TrimMainObjectSearch(db, BaseObjectType.Record);
    trimMainObjectSearch .SearchString = "container:[engagementnumber:1234 and type:recTypeUri]";
    Console.WriteLine(trimMainObjectSearch.Count);

    This search string will find you 'contents' where the container is an item that has an engagement number of 1234 and a record type URI of recTypeUri

    One thing to note though, is this only picks up 'one level deep' of contents. If you have a Folder, that has Dividers/SubFolders, and then documents underneath, this would only get you the 'directly contained' items.

    -Scotty