TexasSwede
texasswede@gmail.com
  • About this blog
  • My Website
  • My Resume
  • XML Export Tool
  • Photos

Free Code – Clean groups in Domino Directory

Posted on December 16, 2015 by Karl-Henry Martinsson Posted in Lotusscript, Programming 8 Comments

The other day I had to clean up the groups in our Domino Directory. Many groups still contained names of terminated users, they had not been cleaned/maintained properly. We also removed some groups over time, but some of them were still listed as members of other groups.

So I wrote a small Lotuscript agent to perform this cleanup. I am posting it below in case someone needs it. As always: no guarantees, use at your own risk, etc.

Update: As some commenters pointed out, there are some issues with the code. First of all, I was using a view my company added to name.nsf. The view PeopleByFirstName has the following selection formula: SELECT Type = “Person” & TerminationDate=””
The field TerminationDate is one we added to the person document. This field is either blank (for current employees) or contains the date they were terminated (and then they are filtered out of most views).

Also as Christian points out, any groups that contains external users would be clenaed out. In the past when I worked at a company that used mail groups with external users, those groups were kept in a separate (secondary) Domino Directory, names_ext.nsf. So this is something to keep in mind if you choose to use this code.

Finally I have updated the code with a few more lines. It will now ignore server groups, and also get a list of all servers and make sure they are included as group members when the groups are processed.

Thanks for pointing out the problems with the code!

 

Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim userNames List As String
Dim groupNames List As String
Dim serverNames List As String
Dim groups List As NotesDocument
Dim tmp As String
Dim tmpname As NotesName
	
Set db = New NotesDatabasesession.CurrentDatabase.Server,"names.nsf")
'*** Get all existing groups and put in a list
Set view = db.getView("Groups")
Set doc = view.GetFirstDocument()
Do Until doc Is Nothing
    Set groups(doc.UniversalID) = doc
    tmp = doc.GetItemValue("ListName")(0)
    groupNames(tmp) = tmp
    Set doc = view.GetNextDocument(doc)	
Loop

'*** Get all active users and put in a list
Set view = db.getView("PeopleByFirstName")
Set doc = view.GetFirstDocument()
Do Until doc Is Nothing
    tmp = doc.GetItemValue("FullName")(0)
    Set tmpname = New NotesName(tmp)
    userNames(tmpname.Common) = tmpname.Common
    Set doc = view.GetNextDocument(doc)	
Loop

'*** Get all servers and put in a list
Set view = db.getView("Servers")
Set doc = view.GetFirstDocument()
Do Until doc Is Nothing
    tmp = doc.GetItemValue("ServerName")(0)
    Set tmpname = New NotesName(tmp)
    serverNames(tmpsname.Common) = tmpname.Common
    Set doc = view.GetNextDocument(doc) 
Loop	
'*** Loop though list of groups and process each one
ForAll g In groups
    '*** Check that not server group
    If g.GetItemValue("GroupType")(0)<>"4" Then  
        tmp = ""
        '*** Store existing list of group members in a backup field
        Call g.ReplaceItemValue("MembersBackup",g.GetItemValue("Members")) 
        '*** Loop through all members of the group
        ForAll m In g.GetItemValue("Members")
            Set tmpname = New NotesName(m)
            '*** If the name is an existing group or an active user, add to list
            If IsElement(userNames(tmpname.Common)) _
            Or IsElement(groupNames(tmpname.Common)) _
            Or IsElement(serverNames(tmpname.Common))Then
                tmp = tmp + tmpname.Canonical + ";"
            End If
        End ForAll
        '*** Write list back to members field
        Call g.ReplaceItemValue("Members", FullTrim(Split(tmp,";")))
        Call g.Save(True,False)
    End If
End ForAll
MsgBox "Done!"

 

« Connect 2016 – People, not technology
My session at IBM Connect 2016 »

8 thoughts on “Free Code – Clean groups in Domino Directory”

  1. Lars Berntrop-Bos says:
    December 16, 2015 at 17:46

    Several problems with the code:
    It uses a nonstandard view which is not in a default NAB, ‘PeopleByFirstName’. If I use ($People), it will empty the server groups ‘LocalDomainServers’ and ‘TravelerServers’. Can you please provide the Selection formula for the view ‘PeopleByFirstName’?

    Reply
    • Karl-Henry Martinsson says:
      December 16, 2015 at 18:01

      Ah, I did not think about that, it’s a view we created internally. Will update the code tomorrow.

      Reply
  2. Chrsitian Henseler says:
    December 17, 2015 at 05:38

    so, your script will delete any external users (from Mail lists) and any Notes users from other domains…

    Reply
  3. Christian Henseler says:
    December 17, 2015 at 05:40

    … and it is not able to deal with Wildcards (e.g in Access only lists), so they will be deleted, too

    Reply
  4. Christian Henseler says:
    December 17, 2015 at 05:41

    … and Servers are also gone from Access lists (assuming your internal view is persons only)

    Reply
  5. Matt Holthe says:
    December 17, 2015 at 14:41

    One thing I do when using Lists…. If I only care about the tag (like “userNames” and “serverNames” in your code), then I define the List with a data type of Byte and always set the value to 1. A Byte data type is 1 byte in size and a String data type is 2 bytes per character in size. This can save a lot of memory as the size of the List grows.

    Nothing wrong with what you’re doing, but that’s just a thing I do when I only need the tag for “IsElement” purposes.

    Reply
    • Karl-Henry Martinsson says:
      December 17, 2015 at 14:44

      Very true. Often I use Boolean as the data type in that case.

      Reply
      • Lars Berntrop-Bos says:
        December 17, 2015 at 15:12

        Booleans are stored as 16bit. So if storage is of the essence, then byte is smallest.

        Then again, if processing a NAB with a lot of groups, storing them all as NotesDocument objects can get you into trouble as well…

        Thats why Bill Buchan and others recommend not keeping NotesDocuments (or other product objects) in memory but rather custom classes. In lists if theres more of them. Also much quicker when manipulating them, I’ve seen agents crawling along using NotesItem updates on NotesDocument objects, which became speed demons just reading the data needed, then do all processing outside the Notes* objects, only going back once all processing is done.

        here’s code which ignores Servergroups, lets external users be, and also ignores group members with a wildcard (*):
        %REM
        Agent check groups for old users
        Created Dec 16, 2015 by L. Berntrop-Bos/Alphen/Zeeman
        Description: checks for groups containing domain users no longer active
        %END REM
        ‘ Option Public ‘ deprecated
        Option Declare

        %REM
        Class group
        Description: holds group data
        %END REM
        Class group
        Public listName As String
        Public members As Variant
        Public membersBackup As Variant
        Public universalID As String
        Public namesRemoved List As String
        %REM
        Sub New
        %END REM
        Sub New(ve As NotesViewEntry, tListName$)
        listName = tListname
        members = FullTrim(ve.Document.GetItemValue(“Members”))
        membersBackup = members
        universalID = ve.Universalid
        End Sub
        End Class
        Sub Initialize
        Dim session As New NotesSession
        Dim dbList List As NotesDatabase
        Dim db As NotesDatabase
        Const test = True

        If test Then
        Set db = session.GetDatabase(“”, “namesZeeman.nsf”)
        Set dbList(db.Server + “!!” + db.FilePath) = db
        Else
        ForAll pNAB In session.Addressbooks
        Set db = pNAB
        If db.IsPublicAddressBook Then
        Call db.Open(“”, “”)
        Set dbList(db.Server + “!!” + db.FilePath) = db
        End If
        End ForAll
        End If

        ForAll pubNAB In dbList
        Call checkNAB(pubNAB)
        End ForAll

        Print “Done!”
        End Sub

        %REM
        Sub checkNAB
        Description: process one NAB
        %END REM
        Sub checkNAB(db As NotesDatabase)
        Dim nServer As NotesName
        Dim view As NotesView
        Dim vec As NotesViewEntryCollection
        Dim ve As NotesViewEntry
        Dim doc As NotesDocument
        Dim userNames List As String
        Dim groupNames List As String
        Dim groups List As group
        Dim saveGroups List As group
        Dim tmp As String
        Dim uname As NotesName
        Dim needsSave As Boolean
        Dim savedOK As Boolean
        Dim allOK As Boolean
        Dim ctUpdate As Long
        Dim v As Variant
        Dim namesRemoved List As Long
        Dim lowOrg As String

        If db.Server = “” Then ‘ if running local, get organiztion from current user
        Set nServer = New NotesName(db.Parent.EffectiveUsername)
        Else ‘ get the org from the server
        Set nServer = New NotesName(db.Server)
        End If
        lowOrg = LCase$(nServer.Organization)

        ‘*** Get all existing groups and put in a list
        Set view = db.GetView(“Groups”) ‘ view ignores Deny Access groups
        Set vec = view.AllEntries
        Set ve = vec.GetfirstEntry
        Do Until ve Is Nothing
        If ve.IsValid And ve.IsDocument And Not ve.IsConflict Then
        If Not CStr(ve.Document.GetItemValue(“GroupType”)(0))=”4″ Then ‘ ignore server groups
        tmp = LCase$(ve.ColumnValues(1)) ‘ groupnames are case insensitive
        groupNames(tmp) = tmp
        Set groups(ve.UniversalID) = New group(ve, tmp)
        End If
        End If
        Set ve = vec.GetNextEntry(ve)
        Loop

        ‘*** Get all active users and put in a list
        Set view = db.GetView(“($PeopleGroupsHier)”)
        Set vec = view.AllEntries
        Set ve = vec.GetfirstEntry
        Do Until ve Is Nothing
        If ve.IsValid And ve.IsDocument And Not ve.IsConflict Then
        v = ve.ColumnValues(4)
        If IsArray(v) Then
        Set uname = New NotesName(v(0)) ‘ (“FullName”)(0)
        Else
        Set uname = New NotesName(v) ‘ (“FullName”)(0)
        End If

        tmp = LCase$(uName.Common)
        userNames(tmp) = tmp
        End If
        Set ve = vec.GetNextEntry(ve)
        Loop
        allOK = True
        ‘*** Loop though list of groups and process each one
        ForAll g In groups
        needsSave = False
        ‘ *** Loop through all members of the group
        If g.members(0) “” Then ‘ skip empty groups
        ForAll m In g.Members
        Set uname = New NotesName(m)
        If LCase(uname.Organization) = lowOrg Then ‘ filter members in our organization (i.e. ignore external users)
        tmp = LCase$(uName.Common)
        ‘*** If the name is not an existing group and not an active user, remove unless it has a wildcard
        If Not IsElement(userNames(tmp)) And Not IsElement(groupNames(tmp)) And InStr(m, “*”) = 0 Then
        tmp = uName.Abbreviated
        g.namesRemoved(tmp) = tmp ‘ keep score in group

        If IsElement(namesRemoved(tmp)) Then ‘ also keep global score
        namesRemoved(tmp) = namesRemoved(tmp) + 1
        Else
        namesRemoved(tmp) = 1
        End If

        NeedsSave = True
        m = “”
        End If
        End If
        End ForAll
        ‘*** Write list back to members field
        If needsSave Then
        g.members = FullTrim(g.Members)
        Set saveGroups(g.universalID) = g
        ctUpdate = ctUpdate + 1
        End If
        End If
        End ForAll
        ‘ Optionally, insert code to display the lists here
        ‘ for now, I send me a log of the proposed actions
        Dim lg As New NotesLog(“log for group check”)
        lg.OpenMailLog db.Parent.EffectiveUsername, “log for group check”

        lg.LogAction “Nab on ” + db.Server + ” !! ” + db.FilePath
        lg.LogAction “.”
        lg.LogAction “.”
        lg.LogAction “NamesRemoved (name: numTimes)”
        lg.LogAction “.”
        ForAll nm In namesremoved
        lg.Logaction ListTag(nm) + “: ” + CStr(nm)
        End ForAll
        lg.LogAction “.”
        lg.LogAction “.”
        lg.LogAction “Groups affected (groupname: removedName(s))”
        lg.LogAction “.”
        ForAll sg In saveGroups
        tmp = “”
        ForAll nmr In sg.namesRemoved
        tmp = tmp + nmr + “; ”
        End ForAll
        lg.Logaction sg.Listname + “: ” + tmp
        End ForAll

        lg.Close

        If MsgBox(“Do you want to update ” + CStr(ctUpdate) + ” Group documents in the NAB?”, 4, “Group document update confirmation”) = 6 Then
        ForAll g In saveGroups
        Set doc = db.GetDocumentByUnid(g.universalID)
        Call doc.ReplaceItemValue(“Members”, g.Members) ‘ clear out empties
        Call doc.ReplaceItemValue(“MembersBackup”, g.membersBackup)
        savedOK = doc.Save(False, False)
        If Not savedOK Then
        allOK = False
        Print “Rerun needed to update group: ” + g.listName
        End If
        End ForAll
        End If
        If Not allOK Then MsgBox “One or more groups were not updated because they could not be saved. Please try again.”
        End Sub

        Reply

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stack Exchange

profile for Karl-Henry Martinsson on Stack Exchange, a network of free, community-driven Q&A sites

Recent Posts

  • Domino 14 is now available
  • Domino 14 Early Access Program
  • Announced: Engage 2024
  • Integrate Node-RED with Notes and Domino
  • Notes and Domino v12 is here!

Recent Comments

  • Theo Heselmans on Announced: Engage 2024
  • Lotus Script Multi-thread Message Box [SOLVED] – Wanted Solution on ProgressBar class for Lotusscript
  • Viet Nguyen on Keep up with COVID-19 though Domino!
  • Viet Nguyen on Keep up with COVID-19 though Domino!
  • Mark Sullivan on Looking for a HP calculator? Look no further!

My Pages

  • How to write better code in Notes

Archives

  • December 2023 (1)
  • October 2023 (2)
  • September 2023 (1)
  • June 2021 (1)
  • April 2021 (2)
  • March 2021 (1)
  • August 2020 (3)
  • July 2020 (2)
  • April 2020 (2)
  • March 2020 (1)
  • December 2019 (2)
  • September 2019 (1)
  • August 2019 (2)
  • July 2019 (2)
  • June 2019 (3)
  • April 2019 (2)
  • December 2018 (1)
  • November 2018 (1)
  • October 2018 (5)
  • August 2018 (2)
  • July 2018 (3)
  • June 2018 (2)
  • May 2018 (1)
  • April 2018 (2)
  • March 2018 (1)
  • February 2018 (2)
  • January 2018 (4)
  • December 2017 (3)
  • November 2017 (2)
  • October 2017 (2)
  • September 2017 (1)
  • August 2017 (2)
  • July 2017 (6)
  • May 2017 (4)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (2)
  • October 2016 (3)
  • September 2016 (4)
  • August 2016 (1)
  • July 2016 (2)
  • June 2016 (2)
  • May 2016 (3)
  • April 2016 (1)
  • March 2016 (4)
  • February 2016 (2)
  • January 2016 (4)
  • December 2015 (3)
  • November 2015 (2)
  • October 2015 (1)
  • September 2015 (2)
  • August 2015 (1)
  • July 2015 (5)
  • June 2015 (2)
  • April 2015 (2)
  • March 2015 (3)
  • February 2015 (2)
  • January 2015 (10)
  • December 2014 (1)
  • November 2014 (3)
  • October 2014 (3)
  • September 2014 (13)
  • August 2014 (6)
  • July 2014 (5)
  • May 2014 (3)
  • March 2014 (2)
  • January 2014 (10)
  • December 2013 (5)
  • November 2013 (2)
  • October 2013 (5)
  • September 2013 (4)
  • August 2013 (7)
  • July 2013 (3)
  • June 2013 (1)
  • May 2013 (4)
  • April 2013 (7)
  • March 2013 (8)
  • February 2013 (9)
  • January 2013 (5)
  • December 2012 (7)
  • November 2012 (13)
  • October 2012 (10)
  • September 2012 (2)
  • August 2012 (1)
  • July 2012 (1)
  • June 2012 (3)
  • May 2012 (11)
  • April 2012 (3)
  • March 2012 (2)
  • February 2012 (5)
  • January 2012 (14)
  • December 2011 (4)
  • November 2011 (7)
  • October 2011 (8)
  • August 2011 (4)
  • July 2011 (1)
  • June 2011 (2)
  • May 2011 (4)
  • April 2011 (4)
  • March 2011 (7)
  • February 2011 (5)
  • January 2011 (17)
  • December 2010 (9)
  • November 2010 (21)
  • October 2010 (4)
  • September 2010 (2)
  • July 2010 (3)
  • June 2010 (2)
  • May 2010 (3)
  • April 2010 (8)
  • March 2010 (3)
  • January 2010 (5)
  • November 2009 (4)
  • October 2009 (7)
  • September 2009 (1)
  • August 2009 (7)
  • July 2009 (1)
  • June 2009 (4)
  • May 2009 (1)
  • April 2009 (1)
  • February 2009 (1)
  • January 2009 (3)
  • December 2008 (1)
  • November 2008 (1)
  • October 2008 (7)
  • September 2008 (7)
  • August 2008 (6)
  • July 2008 (5)
  • June 2008 (2)
  • May 2008 (5)
  • April 2008 (4)
  • March 2008 (11)
  • February 2008 (10)
  • January 2008 (8)

Categories

  • AppDev (10)
  • Blogging (11)
    • WordPress (5)
  • Design (5)
    • Graphics (1)
    • UI/UX (2)
  • Featured (5)
  • Financial (2)
  • Food (5)
    • Baking (3)
    • Cooking (3)
  • Generic (11)
  • History (5)
  • Hobbies (10)
    • LEGO (4)
    • Photography (4)
  • Humor (1)
  • IBM/Lotus (178)
    • #Domino2025 (14)
    • #DominoForever (8)
    • #IBMChampion (46)
    • Administration (7)
    • Cloud (7)
    • CollabSphere (9)
    • Community (49)
    • Connect (33)
    • ConnectED (12)
    • Connections (3)
    • HCL (15)
    • HCL Master (1)
    • IBM Think (1)
    • Lotusphere (46)
    • MWLUG (25)
    • Notes/Domino (99)
      • Domino 11 (7)
    • Sametime (8)
    • Verse (14)
    • Volt (3)
    • Watson (6)
  • Life (8)
  • Microsoft (7)
    • .NET (2)
    • C# (1)
    • Visual Studio (1)
  • Movies (3)
  • Old Blog Post (259)
  • Personal (23)
  • Programming (84)
    • App Modernization (11)
    • Formula (4)
    • Lotusscript (47)
    • NetSuite (4)
      • SuiteScript (3)
    • node.js (4)
    • XPages (4)
  • Reviews (9)
  • Sci-Fi (4)
  • Software (24)
    • Flight Simulator (2)
    • Games (4)
    • Open Source (2)
    • Utilities (6)
  • Technology (37)
    • Aviation (3)
    • Calculators (2)
    • Computers (6)
    • Gadgets (7)
    • Mobile Phones (7)
    • Science (3)
    • Tablets (2)
  • Travel (7)
    • Europe (1)
    • Texas (2)
    • United States (1)
  • Uncategorized (16)
  • Web Development (50)
    • Frameworks (23)
      • Bootstrap (14)
    • HTML/CSS (12)
    • Javascript (32)
      • jQuery (23)

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Tracking

Creeper
MediaCreeper
  • Family Pictures
© TexasSwede 2008-2014