Super Contributor.. AldoH Super Contributor..
Super Contributor..
354 views

How can I count the number of documents inside a folder?

Hello,

I need to know if it is possible to count the amount of documents that are stored in a folder.

I explain to you what I need to do.

I will have to create an event in which I change the title of the documents that are added to a folder. These documents are electronic (word, excel and pdf mostly). The event must be executed every time a new document is added and basically a correlative number must be added to the title based on the amount of documents that already exist in the folder.Therefore my questions are:

  1. How can I know when a document is added?
  2. How can I know in which folder the document is being created?
  3. How can I know the amount of documents in the folder?

I have already done some tests and I can change the title of a document when it is viewed, but I can not find what type of event I must validate to determine that a document was created.

Thank you in advance for the help you can give me.

I am a novice with the HPRM 8.3 SDK.

Regards,

Aldo

0 Likes
5 Replies
Super Contributor.. AldoH Super Contributor..
Super Contributor..

Re: How can I count the number of documents inside a folder?

Hello everybody

I have continued to investigate and have found some answers to some of my questions. I share what I have found in case someone can confirm that it is the right solution or if there is a better way to do it.

Detect when a document is created.

This is the if command where I evaluate if a new record is created.

if(eventData.EventType == HP.HPTRIM.SDK.Events.ObjectAdded)

Get name of the container.

I'm using this code to get the name of the container where the new record is created.

record.Container.Name

I still can not determine how I can know how many documents or records are in a container, but I will continue looking for a solution. If anyone knows how to do it, I would really appreciate it if you could give me an idea of how it is done.

I know that this is probably very basic for experienced users, but as I was saying to you, I am new to developing with the HPRM SDK.

Regards,

Aldo

 

 

0 Likes
Shelley Mac Respected Contributor.
Respected Contributor.

Re: How can I count the number of documents inside a folder?

Hi

I dont know how you count via SDK but in the desktop you can right click over a folder and navigate to contained records and then click on the count function and this will tell you how many documents are in a folder.

To be notified when a document is created regardless of container again via the desktop you would create an event and tell HPERM to notify you everytime a new document (record type) was created.

Not sure if this of any help.

Cheers

Shell

0 Likes
Highlighted
Jamie Smith Respected Contributor.
Respected Contributor.

Re: How can I count the number of documents inside a folder?

Hello Aldo,

You need to use a TrimMainObjectSearch to find records in the folder (container).  After creating the TrimMainObjectSearch method, you need to specify a container clause for the search.  You can do this by using the AddSearchClause() member function, or you can specify a search string with SearchString property.

The TRIM desktop client (assuming you have access to it) has a neat feature that help you easily determine the format of such search strings.  Use the advanced Record Search dialog window.  Choose the 'Container' method for the 'Search By' field, and then select a folder record to search by.  Once that is done, use the 'Editor' drop down meny button at the bottom of the window to change it to the 'String' mode.  This will reveal that the syntax is "container:<RecordURI>", so you can set the SearchString property to "Container:"+record.Container.Uri.UriAsString .

Once you've added the clauses or set the search string, you can use the TrimMainObjectSearch.Count property to find out how many records are contained in the container.

Super Contributor.. AldoH Super Contributor..
Super Contributor..

Re: How can I count the number of documents inside a folder?

Hello,

Thanks to all for the help.

Finally I have achieved what I need to do.I share the code of the event and I will appreciate if you can give me suggestions to improve it or tell me if it would be correct as well.

The text that I am saving as a note in the record allows me to have an output and thus make sure that the code generates the correct values. Then I will remove the lines of code that modify the notes of the record.

 
namespace TestRM
{
    public class TestRM : HP.HPTRIM.SDK.TrimEventProcessorAddIn
    {
        public override void ProcessEvent(HP.HPTRIM.SDK.Database db, HP.HPTRIM.SDK.TrimEvent eventData)
        {
            if(eventData.ObjectType == HP.HPTRIM.SDK.BaseObjectTypes.Record)
            {
                if (eventData.EventType == HP.HPTRIM.SDK.Events.ObjectAdded)
                {
                    HP.HPTRIM.SDK.Record record = db.FindTrimObjectByUri(HP.HPTRIM.SDK.BaseObjectTypes.Record, eventData.ObjectUri) as Record;
                    string notestring = "Record type " + record.RecordType.Name + "Container " + record.Container.Name + " Container uri " + record.Container.Uri + " event fired";
                   
                    string titlenew = record.RecordType.Name;
                    long rec = 0;
                    TrimMainObjectSearch objSearch = new TrimMainObjectSearch(db, BaseObjectTypes.Record);
                    objSearch.SetSearchString("Container:" + record.Container.Uri.UriAsString);
                    rec = objSearch.Count;
                    notestring = notestring + " Records found " + rec;
                    // ---------------------------------------------------------------------------------------------
                    record.SetNotes(notestring, NotesUpdateType.AppendWithUserStamp);
                    titlenew = titlenew + " " + rec;
                    record.Title = titlenew;
                    record.Save();
                }
            }
        }
    }
}
 

Again, thank you very much to all

Aldo,

 

 

0 Likes
Jamie Smith Respected Contributor.
Respected Contributor.

Re: How can I count the number of documents inside a folder?

That looks like it should do what you want it to do.  I assume you've tested the code and found it to work.  There is one important thing you need to be aware of.  Event processors process a queue of events in a specific order.  If an event processor encounters an exception processing a particular event, it will halt, and after a delay it will resume trying to process the event that caused the error.

The line 'HP.HPTRIM.SDK.Record record = db.FindTrimObjectByUri(HP.HPTRIM.SDK.BaseObjectTypes.Record, eventData.ObjectUri) as Record;' may return null if the record has somehow been deleted before your event processor processes the event which created it.  This will cause a null reference exception and cause the processor to be caught in an endless cycle of trying to process that event.  Therefore I recommend you add a check if record is null before trying to use it.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.