Sub Click(Source As Button) ' *** notes for global dims used by button *** ' w - uiworkspace, ' theApi - d.doc api ' uiDoc - this current doc's front-end ' all above dims should already be created/initialized during postopen ' theLibrary - global, but set by this button ' userpwd - global, but set by this button ' button working variable dims Dim s As New NotesSession Dim db As NotesDatabase ' this db Dim selectcabid As String ' selected cabinet Dim selectbinderunid As String ' selected binder (1st value used for null test, then used as cycle/loop variable) Dim doc As NotesDocument ' backend of uiDoc Dim bindersItem As NotesItem ' item with binders to cycle Dim bdDoc As NotesDocument ' binder setup doc (this database - retrieved doc of bindersItem unid) Dim bdbindertitle As String ' binder setup doc / theBinder title Dim bdbindercategory As String ' binder setup doc / theBinder category Dim bdbindertype As String ' binder setup doc / theBinder type Dim bdinheritflg As String ' "1" for inherit security, "0" for override and set custom authors and readers Dim bdAuthorsItem As NotesItem ' item in bdDoc w/ list of binder authors, used when inherit security is set to "0" Dim bdReadersItem As NotesItem ' item in bdDoc w/ list of binder readers, used when inherit security is set to "0" Dim bdManagersItem As NotesItem ' item in bdDoc w/ list of binder managers, used when inherit security is set to "0" Dim tmpLst() As String ' reusable working variable for cycling through bdAuthorsItem and bdReadersItem and converting values to abbreviated format Dim tmpcounter As Integer ' reusable counter for use with tmpLst() Dim curnm As String ' current name within bdAuthorsItem or bdReadersItem formatted to Abbreviated Dim bunid As String ' binder UNID - for backend security update Dim bdbrepid As String ' binder database repID - for backend security update Dim bDb As NotesDatabase ' binder database, retrieved using bdbrepid Dim bDoc As NotesDocument ' binder doc in bDb, retrieved using bunid Dim bMgrItem As NotesItem ' binder doc managers item (used to retrieve current managers, to which, we will append our list) Dim whereami As String ' breadcrumbs debug / log string ' ddoc dims Dim theCabinet As Variant ' cabinet selected by user being processed Dim theBinders As Variant ' collection of current existing binders in theCabinet Dim theBinder As Variant ' current binder being processed (either within theBinders or a new added binder) Dim theBinderSecurity As Variant ' theBinder's security Dim theUsers As Variant ' users of cabinet to assigned to binder (in this case we are going to try to shoe horn into the binder some group names. Dim theUser As Variant ' specific user in theUsers being manimulated On Error Goto ErrorHandler ' get working variables Set db = s.CurrentDatabase Set doc = uiDoc.Document Set bindersItem = doc.GetFirstItem("CS_BindersSelected") selectcabid = doc.CS_Cabinet(0) selectbinderunid = bindersItem.Values(0) ' confirm a cabinet is selected, confirm at least one binder selected If (selectcabid = "" Or selectbinderunid="") Then whereami = "Cabinet or binders not selected. Please make sure a cabinet is selected and at least one binder is selected to process." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami Exit Sub End If ' get theCabinet and theBinders objects Set theCabinet = theLibrary.GetCabinetById(selectcabid) If (theCabinet Is Nothing) Then whereami = "Unexpected Error. Unable to retrieve cabinet." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami Exit Sub End If Set theBinders=theCabinet.Binders ' setup log Call uiDoc.FieldSetText("CS_Log", "") whereami = "Adding/updating binders ... " Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami ' loop through list binders to set in current doc and process Forall bindertoset In bindersItem.Values selectbinderunid = Cstr(bindertoset) Set bdDoc = db.GetDocumentByUNID(selectbinderunid) If Not (bdDoc Is Nothing) Then ' get metadata variables bdbindertitle = bdDoc.BD_Title(0) bdbindercategory = bdDoc.BD_Category(0) bdbindertype = bdDoc.BD_Type(0) bdinheritflg = bdDoc.BD_InheritFlag(0) Set bdAuthorsItem = bdDoc.GetFirstItem("BD_Authors") Set bdReadersItem = bdDoc.GetFirstItem("BD_Readers") Set bdManagersItem = bdDoc.GetFirstItem("BD_Managers") ' check if either authors or readers field values are nothing If (bdAuthorsItem.Values(0)="" And bdReadersItem.Values(0)="") Then ' reset bdinheritflg to "1" to inherit security bdinheritflg="1" End If ' check if binder already exists in selected cabinet Set theBinder = GetBinderByName (theBinders, bdbindertitle) If (theBinder Is Nothing) Then ' need to create a new binder, it doesn't exist whereami = "Creating binder " & bdbindertitle & " ." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami ' create the binder Set theBinder = theBinders.Add Else ' binder exists, make sure we have the working copy whereami = "Updating binder " & bdbindertitle & " ." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami ' check out and update the existing binder Call theBinder.SetToWorkingCopy End If ' update metadata of new/existing binder ... ' add title theBinder.Title = bdbindertitle ' Set the binder type to Categorized Binder & binder category Set theBinder.Profile = theCabinet.BinderProfiles(bdbindertype) theBinder.Profile.Fields("BinderCategory").value = bdbindercategory ' setup security Set theBinderSecurity = theBinder.Security ' (must first test to see if binder security is disabled) If (theBinderSecurity.IsSecured) Then ' security is turned on, proceeding... If (bdinheritflg="1") Then ' set flags to inherit from cabinet (Both AllReadersFlag & AllEditorsFlag to True) theBinderSecurity.AllEditorsFlag = True theBinderSecurity.AllReadersFlag = True Else ' set flags to set manually theBinderSecurity.AllEditorsFlag = False theBinderSecurity.AllReadersFlag = False ' save and checkin binder Call theBinder.Save Call theBinder.CheckIn(2, "Added by API.") whereami = "Checked-in binder " & bdbindertitle & "." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami ' since security still doesn't work (since vs. 2.5), we need to complete this via backend bunid = theBinder.UniversalId bdbrepid = theCabinet.BinderDatabase Set bDb = New NotesDatabase("", "") If (bDb.OpenByReplicaID(db.Server, bdbrepid)) Then ' database found & opened, get binder doc Set bDoc = bDb.GetDocumentByUNID(bunid) If Not (bDoc Is Nothing) Then ' found binder doc, update security ' - DocManagers, DocAuthors, DocReaders - datatype is Text/Text List. Store names in Abbreviated format. ' - ComputedManagers (Authors datatype), ComputedAuthors, ComputedReaders - (Readers datatype) ' (this script does not implement Manager fields) ' DocReaders tmpcounter = 0 Forall usernm In bdReadersItem.Values curnm = NotesNameReformat(Cstr(usernm), 2) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall Call bDoc.ReplaceItemValue("DocReaders", tmpLst) ' ComputedReaders tmpcounter=0 Forall usernm In bdReadersItem.Values curnm = NotesNameReformat(Cstr(usernm), 1) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = "[Administrator]" ' add role to list Call bDoc.ReplaceItemValue("ComputedReaders", tmpLst) ' DocAuthors tmpcounter = 0 Forall usernm In bdAuthorsItem.Values curnm = NotesNameReformat(Cstr(usernm), 2) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall Call bDoc.ReplaceItemValue("DocAuthors", tmpLst) ' ComputedAuthors tmpcounter = 0 Forall usernm In bdAuthorsItem.Values curnm = NotesNameReformat(Cstr(usernm), 1) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall Call bDoc.ReplaceItemValue("ComputedAuthors", tmpLst) ' DocManagers tmpcounter = 0 ' part 1, get existing values Set bMgrItem = bDoc.GetFirstItem("DocManagers") Forall mgrnm In bdAuthorsItem.Values curnm = NotesNameReformat(Cstr(mgrnm), 2) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall ' part 2, add new values Forall usernm In bdManagersItem.Values curnm = NotesNameReformat(Cstr(usernm), 2) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall Call bDoc.ReplaceItemValue("DocAuthors", tmpLst) ' ComputedManagers tmpcounter = 0 ' part 1, get existing values Set bMgrItem = bDoc.GetFirstItem("ComputedManagers") Forall mgrnm In bdAuthorsItem.Values curnm = NotesNameReformat(Cstr(mgrnm), 1) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall ' part 2, add new values Forall usernm In bdManagersItem.Values curnm = NotesNameReformat(Cstr(usernm), 1) Redim Preserve tmpLst(tmpcounter) As String tmpLst(tmpcounter) = curnm tmpcounter=tmpcounter + 1 End Forall ' (don't need to append role [Administrators], already in original loaded list) Call bDoc.ReplaceItemValue("ComputedAuthors", tmpLst) ' save updated security Call bDoc.Save(True,False) whereami = "Updated binder " & bdbindertitle & " with back-end security update." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami Else ' unable to retrieve binder doc for backend security update whereami = "Unable to retrieve binder document for back-end security update." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami End If Else ' unable to retrieve binder database whereami = "Unable to retrieve binder document's database for back-end for security update." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami End If End If ' end inherit / manual End If ' end issecured test Else ' cannot find binder setup doc whereami = "Skipped binder setup document: " & selectbinderunid & ". Unable to load it." Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami End If End Forall CleanUp: ' clean up Set theUser = Nothing Set theUsers = Nothing Set theBinderSecurity = Nothing Set theBinder=Nothing Set theBinders=Nothing whereami = "Completed all binder creates/updates." Call uiDoc.FieldAppendText("CS_Log", whereami) Print whereami Exit Sub ErrorHandler: whereami = "(Initialize) " & Error$ & " on line " & Cstr(Erl) & "; Last Marker: " & whereami & ". " Call uiDoc.FieldAppendText("CS_Log", whereami & Chr$(13) & Chr$(10)) Print whereami Goto CleanUp Exit Sub End Sub