# Populate dropdown list with users

**URL:** <https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222>\
**Category:** Ignition\
**Created:** [August 24, 2021, 2:10pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222 "2021-08-24T14:10:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 24, 2021, 2:10pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/1 "2021-08-24T14:10:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 24, 2021, 2:21pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/2 "2021-08-24T14:21:11Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 24, 2021, 4:52pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/3 "2021-08-24T16:52:22Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 24, 2021, 5:28pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/4 "2021-08-24T17:28:34Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 25, 2021, 6:59am UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/5 "2021-08-25T06:59:44Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mcgheeiv](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@mcgheeiv](https://forum.inductiveautomation.com/u/mcgheeiv)\
**Post date:** [August 25, 2021, 10:25am UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/6 "2021-08-25T10:25:23Z")

</div>

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_sourc`e and `role_of_user`s with a real user source and role.

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 25, 2021, 12:45pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/7 "2021-08-25T12:45:17Z")

</div>

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

```auto
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

```auto
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:

```auto
  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!

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 25, 2021, 1:47pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/8 "2021-08-25T13:47:29Z")

</div>

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.

```auto
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.

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

```

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 26, 2021, 6:00am UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/9 "2021-08-26T06:00:13Z")

</div>

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](https://forum.inductiveautomation.com/t/bug-on-dropdown-in-editable-selection-mode/44116) ) that the problem has not been solved.

Have you experienced this problem?

---

<div class="post-metadata">

**Author:** ![Farheen\_Qureshi](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Farheen\_Qureshi](https://forum.inductiveautomation.com/u/Farheen_Qureshi)\
**Post date:** [August 27, 2024, 1:42pm UTC](https://forum.inductiveautomation.com/t/populate-dropdown-list-with-users/50222/10 "2024-08-27T13:42:49Z")

</div>

add one "getroles "parameter in your view then do the property binding in option  
then write given script in transform it will work roles = system.user.getRoles("default")  
roleData =   
for role in roles:  
roleObj = {}  
roleObj['label'] = role  
roleObj['value'] = role  
roleData.append(roleObj)  
return roleData
