Programmatically replace the design of Lotus Notes databases

A similar situation arises when trying to programmatically replace the design of a Lotus Notes database. IBM doesn't provide a LotusScript method or C-API function to accomplish this task. In fact, the only way to replace a database design is by manually choosing File - Database - Design Replace for each specific database. However, this manual method doesn't make much sense when replacing the design of several databases. The following LotusScript code can be used to replace the design of multiple databases. The script actually deletes all design notes from the target database and copies them from the source database.
LotusScript


Sub ReplaceDesign(SourceServer As String, SourcePath As String, TargetServer As String, TargetPath As String)
	Dim Session As New NotesSession
	Dim workspace As New NotesuiWorkspace
	
	Dim Sourcedb As New NotesDatabase(SourceServer,SourcePath)
	Dim Targetdb As New NotesDatabase(TargetServer,TargetPath)
	
	'Delete elements from the Target Database 
	'Ignore Elements having "Prohibit design...." property or Master Template Defined
	Dim TargetNoteCol As NotesNoteCollection 
	Set TargetNoteCol = Targetdb.CreateNoteCollection(True)
	
	'Select All Design Elements
	'Deselct Admin Notes 
	Call TargetNoteCol.SelectAllAdminNotes(False)
	'Deselect Document Notes
	Call targetNoteCol.SelectAllDataNotes(False)
	'Deselect Icon Note
	TargetNoteCol.SelectIcon = False
	'Deselect Help-About and Help Using Notes which has different mechanism for checking "Prohibit design....."
	TargetNoteCol.SelectHelpAbout = False
	TargetNoteCol.SelectHelpIndex = False
	TargetNoteCol.SelectHelpUsing = False
	
	
	'Loop Thru All Design Notes and Check -- 'Ignore Elements having "Prohibit design...." property or Master Template Defined
	Call TargetNoteCol.BuildCollection 
	Dim DesignDoc As NotesDocument 
	Dim ID As String 
	Dim NextID As String 
	ID = TargetNoteCol.GetFirstNoteId
	For i = 1 To TargetNoteCol.Count 
		NextID = TargetNoteCol.GetNextNoteId(ID)
		Set DesignDoc = TargetDB.GetDocumentByID(ID)
		If (Not(DesignDoc.HasItem("$Class"))) And Instr(DesignDoc.~$Flags(0),"P") =0 Then
			Print "Removing  " + DesignDoc.~$Title(0) 
			Call DesignDoc.Remove(True)
		End If
		ID= NextID
	Next
	
	'Check for Help About and Help Using 
	
	Dim IsHelpAbout As Boolean 
	Dim IsHelpUsing As Boolean 
	IsHelpAbout = False
	IsHelpUsing = False
	Call TargetNoteCol.SelectAllNotes(False)
	Call TargetNoteCol.ClearCollection
	TargetNoteCol.SelectHelpAbout = True
	Call TargetNoteCol.BuildCollection 
	If TargetNoteCol.Count <> 0 Then
		ID = TargetNoteCol.GetFirstNoteId
		Set DesignDoc = TargetDB.GetDocumentByID(ID)
		If Instr(DesignDoc.~$Flags(0),"R") <> 0 Then
			Print "Removing  Help-About Document"
			Call DesignDoc.Remove(True)
			IsHelpAbout = True
		End If
	End If
	Call TargetNoteCol.SelectAllNotes(False)
	Call TargetNoteCol.ClearCollection
	TargetNoteCol.SelectHelpUsing = True
	Call TargetNoteCol.BuildCollection 
	If TargetNoteCol.Count <> 0 Then
		ID = TargetNoteCol.GetFirstNoteId
		Set DesignDoc = TargetDB.GetDocumentByID(ID)
		If Instr(DesignDoc.~$Flags(0),"R") <> 0 Then
			Print "Removing  Help-Using Document"
			Call DesignDoc.Remove(True)
			IsHelpUsing = True
		End If
	End If
	
	'Set More Fields Options
	Call TargetDb.SetOption(54,True)
	
	'Copy All the Design Elements from SourceDB to Target DB except Help About and Help Using
	Dim SourceNoteCol As NotesNoteCollection 
	Set SourceNoteCol = Sourcedb.CreateNoteCollection(True)
	'Deselct Admin Notes 
	Call SourceNoteCol.SelectAllAdminNotes(False)
	'Deselect Document Notes
	Call SourceNoteCol.SelectAllDataNotes(False)
	'Deselect Icon Note
	SourceNoteCol.SelectIcon = False
	'Deselect Help-About and Help Using Notes which has different mechanism for checking "Prohibit design....."
	
	If IsHelpAbout = False Then
		SourceNoteCol.SelectHelpAbout = False
	End If
	If IsHelpUsing = False Then
		SourceNoteCol.SelectHelpUsing = False
	End If
	
	'Loop Thru All Design Notes and Check -- 'Ignore Elements having "Prohibit design...." property or Master Template Defined
	Call SourceNoteCol.BuildCollection 
	ID = SourceNoteCol.GetFirstNoteId
	For i = 1 To SourceNoteCol.Count 
		NextID = SourceNoteCol.GetNextNoteId(ID)
		Set DesignDoc = SourceDB.GetDocumentByID(ID)
		Call DesignDoc.CopyToDatabase(TargetDB)		
		Print "Copying...." + DesignDoc.~$Title(0) 
		ID= NextID
	Next
	
	
	'Add All the roles from Source DB to Target DB
	Dim TargetACl As NotesACL 
	Dim SourceACL As NotesACL 
	Set TargetACL = TargetDb.ACL 
	Set SourceACL = SourceDB.ACL 
	Dim TargetRoles As Variant 
	Dim SourceRoles As Variant 
	TargetRoles = TargetACL.Roles 
	SourceRoles = SourceACL.Roles 
	If  Isarray(SourceRoles) Then
		Dim RolesToBeAdded As Variant 
		RolesToBeAdded = Arrayunique((Arrayreplace(SourceRoles,TargetRoles,"")))
		Forall DummyRole In RolesToBeAdded
			If Fulltrim(DummyRole) <> "" Then
				TargetACL.AddRole(Strleft(Strright(DummyRole,"["),"]"))
			End If
		End Forall
		TargetACL.Save
	End If
End Sub

Written by Amith Narera

Posted by fbrefere001 on Wednesday March 5, 2008