Extract peoples from groups in names.nsf(works well with nested groups)

Dear friends,

See this code. This will help you in extracting people from group entries of names.nsf. it will work on nested groups also.

Declarations

Dim count As Integer

Dim persons List As String

Dim groups List

Dim tg As Integer

Dim xy As Integer

Sub Initialize

'The advantate of this code is that we can process any nested groups without any size limitaions.

Dim session As New notessession	

Dim ws As New notesuiworkspace

Dim uidoc As NotesUIDocument



Dim db As notesdatabase

Dim namesdb As notesdatabase

Dim groupdoc As notesdocument

Dim groupview As notesview

Dim insidegroupdoc As notesdocument

Dim insidegroups As notesview

Dim doc As notesdocument

Dim curview As notesview

Dim newlist List



Set db=session.CurrentDatabase

Set namesdb=session.GetDatabase(db.Server,"names.nsf")

Set groupview=namesdb.GetView("($Users)")

Set insidegroups=namesdb.getview("($VIMGroups)")

Set curview=db.getview("second")

Set uidoc=ws.CurrentDocument



Set doc=curview.getfirstdocument	'you can set your own doc here.  This the backend document for the currently opened uidoc.  You can find other alternatives to run this agent.

groupName$ =uidoc.FieldGetText("names")		' Here we are entering the group or person names.



Dim ritem As Variant

Set ritem = doc.GetFirstItem( "all" ) ' Here we are going to store the group extracted people's name



'Call uidoc.FieldSetText("saveoptions","1")

Call uidoc.FieldSetText("Total","") ' Here we will list the total participant list.

Call uidoc.FieldSetText("all","") ' making the field null to print a new value.

Call uidoc.Save

Call uidoc.reload

Call uidoc.save	



Forall entry In doc.names

	gtype=group(entry)			'Checking whether the entry is a group or People.

	If gtype="Group"	 Then

		tg=tg+1

		groups(tg)=entry

	Else			

		Call addtolist(entry) ' If entry is a person, then adding to a list called People.

	End If

End Forall	

'now we have got two lists one is person and another one is group

'This loop will returns you a list in the people’s view. and remove the each group entry after processing.

xy=0

Do Until xy = tg

	xy=xy+1		

	et=groupget(groupview,groups(xy))	

	Erase groups(xy)

Loop



If ( ritem.Type <> RICHTEXT ) Then

	Exit Sub

End If

'Iam adding the list to another array, because iam not getting the list with unique values. You can try some other code if you can retruns the list as unique

Dim nar() As String

Forall nvals In Persons

	newcount=newcount+1

	Redim Preserve nar(newcount)

	nar(newcount)=Persons(newcount)

End Forall

'Then make the array as unique

finarray=Arrayunique(nar,0)	

'Now print the values one by one in an array. See if you want to process the contents of this rtfield.

’ Use the item.values and split the values using newline. So you will get an array.

Forall fins In finarray

	If fins <> "" Then

		fin=fin+1				

		Call ritem.Appendtext(fins)

		Call ritem.addnewline(1)

	End If

End Forall			

'This code will close the doc without saving and re-open it.

Call doc.ReplaceItemValue("Total",Cstr(fin))

Call doc.save(True,True)

doc.saveoptions="0"

Call uidoc.close

Call ws.EditDocument(False,doc)		

'You can use the notesname class to format the contents and use the evaluate to sort the contents. The code you can get it from my 'Other agent in the same db.

End Sub

Function group(entry As Variant) As String

'Getting first 3 characters will give u an idea whether it is a group or person. In our case every user will have a heirarhical name. In your case you can use ur own code to get this info.

If Left(entry,3)="CN=" Then

	group="Person"		

Else

	group="Group"					

End If

End Function

Function addtolist(entry As Variant)

count=count+1

If Isarray(entry) Then ' In some rare cases by mistake or so we have two entries in a document.  So run this check before proceed.

	Forall ad In entry

		persons(count)=ad

	End Forall

Else

	persons(count)=entry

End If	

End Function

Function groupget(gview As notesview,entry As Variant) As String

'here first we are getting group name. Then get the group document and get the members for the group. Then add the person entries to Person array.

'Rest group entries will add to group list.

Set groupdoc=gview.GetDocumentByKey(entry,True)	

If Not groupdoc Is Nothing Then	

	allentries=groupdoc.GetItemValue("members")

	Forall ent In allentries

		'Set pdoc=gview.GetDocumentByKey(entry,True)	

		If Left(ent,3)="CN=" Then	

			count=count+1

			persons(count)=ent

		Else			

			tg=tg+1

			groups(tg)=ent

			ab=ab+1

		End If					

	End Forall

End If

End Function

Subject: Extract peoples from groups in names.nsf(works well with nested groups)

your code seems to work well.however you could also look here :

http://www-10.lotus.com/ldd/nd6forum.nsf/ShowMyTopicsAllThreadedweb/183db23b9d55c15385256f33004f88fd?OpenDocument

cheers !!

Ashish

Subject: RE: Extract peoples from groups in names.nsf(works well with nested groups)

Hi ,thanks for the comments. I have achieved this functionality using the Evaluate( |@ExpandNameList(@Dbname;“| & groupName$ & |”)| ) earlier. But i found some issues with that. One is the size limitation. In EY we have some groups where members are more with nested groups. So it will crash. i have used another alternative for this using split up of groups and then i provided this “Evaluate( |@ExpandNameList(@Dbname;”| & groupName$ & |“)| )”. Still it failed. Then i wrote this code. This @expandnamelist cannot process on large groups.

let me know your comments boss

Subject: RE: Extract peoples from groups in names.nsf(works well with nested groups)

Hi Prasad,You are right. I had posted this tip on Search Domino too, and yesterday one of the users commented on the same issue. I have used it quite some times and the nesting levels have been quite decent and within its capacity … i guess. I have never had the opportunity to try it on exhasutive nested groups and hence i was not aware.

Thanks for ur suggestion,

take care,

Ashish

Subject: RE: Extract peoples from groups in names.nsf(works well with nested groups)

Hi Ashish,

It was me that posted that a comment on search domino. I also wanted to clear the doubts. Anyway i will take all these opportunities as a good learning experience for all of us. I have tested this in big groups and it failed. We have around 1,00,000 employees all around the world and we have big nested groups. so i got an opportunity to test this on those global groups. I wondering why notes is not coming up with any functions or scripts for the same functionlity.

you can contact me on prasad.kl@ae.ey.com

Thanks and do keep up the good work.

Subject: Why you can’t always trust LS when Extracting users in Groups

There is a problem depending on your purpose for having to expand the values in a group. I wrote similar code to determine all names of users in groups and subgroups for mailing purposes. The problem is that the code does not account for CONFLICTS of addresses for email purposes. The formula language however, DOES account for the conflicts.

Here is the problem:

If you have a group name (e.g., ‘USA’) that matches a user’s first name (e.g., user ‘USA Generic’), then if that group is defined as a subgroup (e.g., within ‘All Countries’), they will not get emailed because of the conflict of having duplicate names.

These conflicts (for my purposes) are not noticed by the Lotusscript code. However, if you use the formula language, you CAN use that to determine whether or not a user will be emailed when a group name is used, because it does seem to recognize a conflict also under those circumstances.

In addition, I modify my LS code to display the child group the user is extracted from, in order to troubleshoot existing name conflicts that might exist, and to determine if the user is in multiple child groups under the primary group you are researching.

Subject: RE: Why you can’t always trust LS when Extracting users in Groups

While you are supplying the user names to this code, allways pass an username value with full heirarchical name. If not then get the heirarchical name from names.nsf through code and pass it to this function. it will work. If the user name and group names are same, then there are no heirarchical name for groups so it wont take it as a user.

thanks

Subject: RE: Why you can’t always trust LS when Extracting users in Groups

I’m not sure you’re clear on what I was saying. For emailing purposes, if a group name matches the first or last name of a user, then there will be a conflict when that group is used as a subgroup of another. It doesn’t matter if that user is in the group or not, in any format. They are in the directory, which is what matters. It is in resolving the subgroups that the conflict is not resolved in Lotusscript code, unless you add additional code to assume there is a conflict under matching circumstances by adding a lookup to all first and last names when you encounter a subgroup.

Subject: Extract peoples from groups in names.nsf(works well with nested groups)

This was a great help. Thanks for posting it!

I’m adapting it to a backend class script library for a project of mine. This involved taking out some of the uidoc stuff and passing values differently into the main sub, but nothing too difficult.