Populate dropdown list with users

Good morning guys,

I’m trying to figure out how to populate a dropdown list with the users saved into the ignition gateway but I can’t find a way to do it. The idea is to populate it when the log in page is opened.

I can’t find an example to start from, could you kindly help me please?

Thank you very much.

I have a function in a shared script so I can use it from any project.

def getEmployees(source, role):
	'''
	returns a datset of user names based on the user source and role filters
	'''
	
	header = ['user']
	opList = []
	exceptions = ['autologin', 'Test User', 'admin']
	users = system.user.getUsers(source)
	for user in users:
		roles = user.getRoles()
		if role in roles:
			username = user.get('username')
			if username not in exceptions:
				opList.append([username])
	ds = system.dataset.toDataSet(header,opList)
	return ds
2 Likes

@dkhayes117 , thank you very much for your reply!

This is very useful, now I have to figure out how to use a shared script. Is right to creare the script in “Scripting” → “Project library” or is the wrong path?

what version are you on? for v7.9 you have a global shared script. In v8 you have a global inherited project that stores your global scripts. Otherwise, it is a project script.

for v7.9 its like

shared.<yourScriptNameHere>.<functionName>
IE shared.users.getEmployees(<anyVariablesUsedHere>)
or 
project.users.getEmployees()

https://docs.inductiveautomation.com/display/DOC81/Scripting+in+Ignition#ScriptinginIgnition-ProjectScripts

I’m using the 8.1 version.

I’m trying to using the library but I can’t make it work, sorry but is the first time that I use a library.

I’m in the script console to make some simple tests, for example

print UserList.getEmployees()

this is not working because getEmployees() takes 2 arguments, but I don’t know how to define the two arguments.

Could you help me once more to figure out how to make it work?

Thank you very much for your time.

The first argument is the source of the users as defined in the Gateway User Sources. Every gateway starts out with a “default” source, more may have been added to your gateway.

The second argument filters on users based on a specific role. For example, if “Administrator” is used as the second argument, the function will return users that have the role Administrator.

The proper way to call the function is getEmployees('my_user_source', 'role_of_users')

replacing my_user_source and role_of_users with a real user source and role.

Thanks for the example, now it’s working… I’ve just modified the script in this way:

def getEmployees(source):
	'''
	returns a datset of user names based on the user source and role filters
	'''
	
	header = ['user']
	opList = []
	
	users = system.user.getUsers(source)
	for user in users:
		roles = user.getRoles()
		if not 'Administrator' in roles and not 'Operatore' in roles:
			username = user.get('username')
			opList.append([username])
	
	ds = system.dataset.toDataSet(header,opList)
	return ds

In this way I show only roles that aren’t Administrator or Operatore, is correct as I modified or it should be done better?

Another question, I put the follow code to populate a dropdown when on in the visionWindowOpened of my login page

ds = users.getEmployees('default')
event.source.parent.getComponent('Dropdown').data = ds

the function works properly and it populate the dropdown, but ignition report the follow error:

  File "<event:visionWindowOpened>", line 2, in <module>
TypeError: getComponent(): 1st arg can't be coerced to int

Where is the mistake?

Thank you very much guys!

Your 2nd line is when you are scripting from the root container. You are calling the script from the window event so use this instead.

system.gui.getParentWindow(event).getComponentForPath('Root Container.Dropdown').data = ds

Alternately, you could put the script as you had it in the dropdown itself under property change event handler.

if event.propertyName == 'componentRunning':
    event.source.data = users.getEmployees('default')

You are right dkhayes117.

I take this opportunity to ask if you have the same problem I’m having with the Dropdown; now I’m populating the dropdown with your script and everybody can choose their own user, but I need to insert manually another user if is needed (for example Admin), so I change the Selection Mode to Editable, but when I start to write the first letter, the dropdown choose automaticly the first word already present in its data and I’m not free to insert what I want. I red in this link ( Bug on dropdown in editable selection mode ) that the problem has not been solved.

Have you experienced this problem?