During our work on a web application, we were required to show different documents to the users based on different values of the form's "Status" field. This was achieved by creating as many views as the "Status" field can possess. However, this leads to a large increase in the size of database. In addition, it may have an effect on the performance of application.
To achieve our goal without making so many views, we thought of some dynamic view or dynamic view selection criteria. This way, we only to create two views. Following are the steps to achieve the functionality.
1. Create the view (say,"ViewByStatus") in which we want to show documents dynamically based on the value of the "Status" field. 2. In the view selection formula of "ViewByStatus," we are adding code "& @Text(Status) = @Text(statusDyn)". 3. Create a new view (say,"ViewDyn") with first sorted column (key column) having selection as "Status" field. 4. Create a new hidden field (say "StatusDyn") on the form containing "Status" field. 5. On the buttons or links (there'll be one button/link for each dynamic set of documents in the ViewByStatus) through which we are opening our dynamic view ("ViewByStatus"), modify the code to open the agent (say "DynamicViewSelection") by the following code: "./DynamicViewSelection?openagent&Status=X" where "x" is the status value we are passing to our agent. 6. Name of the agent is "DynamicViewSelection." The code is written and described below.
Sub Initialize Dim Session As New NotesSession Dim doc,notesdoc As notesdocument Dim db As notesdatabase Dim View As Notesview Dim entrycoll As notesviewentrycollection Dim notesentry As notesviewentry Dim ProfileDoc As NotesDocument Dim coll As notesdocumentcollection Dim i,j As Integer Dim tmp As Variant Dim ID,statusDyn As Integer ' Setting of variables/objects Set db = Session.Currentdatabase Set View=db.getview("ViewDyn") Set doc=session.documentcontext Set ProfileDoc = db.GetProfileDocument("HROnlineParameters") Set entrycoll = View.AllEntries ' Get the value of Query_String in variable "tmp" tmp = doc.QUery_String(0) ' Extract value of status from tmp and set in ID ID = Cint(Right$(tmp,1) ) ' Pick all the documents from the ViewDyn with key value as ID Set coll=View.GetAllDocumentsByKey(ID) ' Pick all the entries from the ViewDyn Set entrycoll=View.AllEntries ' Pick first notes entry from the entry collection, and set doc Set notesentry=entrycoll.getfirstentry Set doc=notesentry.document ' Initialization: Set the value of StatusDyn field on all the documents in entrycoll to null doc.StatusDyn=0 Call doc.save(True,True) For j=2 To entrycoll.count Set notesentry=entrycoll.getnextentry(notesentry) Set doc=notesentry.document doc.StatusDyn=0 Call doc.save(True,True) Next ' Set the value of selected documents in coll to ID Set notesdoc=coll.GetFirstDocument notesdoc.StatusDyn=ID Call notesdoc.save (True,True ) For i=2 To coll.count Set notesdoc=coll.getnextdocument(notesdoc) notesdoc.StatusDyn=ID Call notesdoc.save (True,True ) Next ' Code to open the dynamic view (Workdir is the application directory name, which can be specified from the profile document) Workdir = ProfileDoc.HROnlineDirectory(0) Print "" End Sub
This was first published in June 2001