Create a new database view at run time.
Category lotus none lotusscript
Hey there Hi there Ho There .... Here is one I use quite a bit that creates a new view in a database copied from an existing view. It copies the view, renames the new copy, and changes the selection formula. Pretty handy if you have a database that you want users to have customized view content, but you don't want to monkey with folders, making sure the document is/isn't in the folder, etc. Also, you can create the view from any existing view, not just the default view. It's dependant on Damien Katz's Database Design class, which i've included in the downloads Take a look:
Hey there Hi there Ho There .... Here is one I use quite a bit that creates a new view in a database copied from an existing view. It copies the view, renames the new copy, and changes the selection formula. Pretty handy if you have a database that you want users to have customized view content, but you don't want to monkey with folders, making sure the document is/isn't in the folder, etc. Also, you can create the view from any existing view, not just the default view. It's dependant on Damien Katz's Database Design class, which i've included in the downloads Take a look:
%REM
************************************************************************************************************
PROCEUDRE - NewView() - This sub creates a new view either from a "template" or from the default view.
Parameters
viewName - String - Name of the new view that will be created
PathView - String - Path to new view location
Template - String - Fully Qualified view name that the new view's design will be based off, specify "" to use database default
ThrowErrors - Variant - True | False Whether or not to be verbose on errors such as view already existing, etc
SelectionFormula - String - String of the Selection Formula for the new View
NOTE: When using database default, if the view is nested, and the parent view does not exist it will be created. Specifying a template name will not create nested views if they do not already exist.
%END REM *****************************************************************************************************
Sub Newview(viewName As String, Pathview As String, Template As String, ThrowErrors As Variant, SelectionFormula As String)
Dim Db As NotesDatabase
Dim session As New NotesSession
Set Db = session.CurrentDatabase
If Len(Trim(Pathview)) <> 0 And Right(Trim(Pathview), 1) <> "\" Then ' Add Trailing '\' if not already there and not root view...
Pathview = Trim(Pathview) + "\"
End If
Dim actview As NotesView
Set actview = Db.GetView(Pathview + viewName)
If actview Is Nothing Then ' Check to see if view exists
Dim dbDesign As DatabaseDesign
Set dbDesign = createDatabaseDesign(Db) ' Get Database Design Object
If Len(Trim(Template)) <> 0 Then
Dim defview As NotesDocument ' Get Default Template for view
Set defview = dbDesign.GetViewByName(Template)
Dim Newview As NotesDocument ' Make a copy of the Template
Set Newview = defview.CopyToDatabase(Db)
Call Newview.replaceItemValue( "$Title", Pathview + viewName) ' Change name to new view's Name
' Turn off design inheritance and propagation...
Dim iFlags As NotesItem
Set iFlags = Newview.GetFirstItem("$Flags")
Dim Flags As String
Flags = iFlags.Text
Flags = Flags + DESIGN_FLAG_PRESERVE
Flags = Flags + DESIGN_FLAG_PROPAGATE_NOCHANGE
Call Newview.replaceitemvalue("$Flags", Flags)
Newview.save True, True ' Save the new view
If Trim(SelectionFormula) <> "" Then ' Change the View Selection Formula
Dim NView As NotesView
Set NView = db.GetView(Pathview + viewName)
NView.SelectionFormula = SelectionFormula
End If
Else
Call Db.Enableview(Pathview + viewName) ' Create the view from the default view/view of the database
End If
Else
If ThrowErrors Then ' Do they want to know it already exists? If so Then tell them...
Messagebox "The specified view already exists.", MB_ICONEXCLAMATION + MB_OK, AppName()
End If
End If
End Sub
PROCEUDRE - NewView() - This sub creates a new view either from a "template" or from the default view.
Parameters
viewName - String - Name of the new view that will be created
PathView - String - Path to new view location
Template - String - Fully Qualified view name that the new view's design will be based off, specify "" to use database default
ThrowErrors - Variant - True | False Whether or not to be verbose on errors such as view already existing, etc
SelectionFormula - String - String of the Selection Formula for the new View
NOTE: When using database default, if the view is nested, and the parent view does not exist it will be created. Specifying a template name will not create nested views if they do not already exist.
%END REM *****************************************************************************************************
Sub Newview(viewName As String, Pathview As String, Template As String, ThrowErrors As Variant, SelectionFormula As String)
Dim Db As NotesDatabase
Dim session As New NotesSession
Set Db = session.CurrentDatabase
If Len(Trim(Pathview)) <> 0 And Right(Trim(Pathview), 1) <> "\" Then ' Add Trailing '\' if not already there and not root view...
Pathview = Trim(Pathview) + "\"
End If
Dim actview As NotesView
Set actview = Db.GetView(Pathview + viewName)
If actview Is Nothing Then ' Check to see if view exists
Dim dbDesign As DatabaseDesign
Set dbDesign = createDatabaseDesign(Db) ' Get Database Design Object
If Len(Trim(Template)) <> 0 Then
Dim defview As NotesDocument ' Get Default Template for view
Set defview = dbDesign.GetViewByName(Template)
Dim Newview As NotesDocument ' Make a copy of the Template
Set Newview = defview.CopyToDatabase(Db)
Call Newview.replaceItemValue( "$Title", Pathview + viewName) ' Change name to new view's Name
' Turn off design inheritance and propagation...
Dim iFlags As NotesItem
Set iFlags = Newview.GetFirstItem("$Flags")
Dim Flags As String
Flags = iFlags.Text
Flags = Flags + DESIGN_FLAG_PRESERVE
Flags = Flags + DESIGN_FLAG_PROPAGATE_NOCHANGE
Call Newview.replaceitemvalue("$Flags", Flags)
Newview.save True, True ' Save the new view
If Trim(SelectionFormula) <> "" Then ' Change the View Selection Formula
Dim NView As NotesView
Set NView = db.GetView(Pathview + viewName)
NView.SelectionFormula = SelectionFormula
End If
Else
Call Db.Enableview(Pathview + viewName) ' Create the view from the default view/view of the database
End If
Else
If ThrowErrors Then ' Do they want to know it already exists? If so Then tell them...
Messagebox "The specified view already exists.", MB_ICONEXCLAMATION + MB_OK, AppName()
End If
End If
End Sub
Pretty straightforward ... gets the 'template view', copies it, renames it, sets the Do Not Update on Design Replace/Refresh, and Propagate Do Not update flags, and gets out. Only draw back to this is the Lotus Notes Client won't see the new view until the database is exited and re-opened.
Hasta !!














- 