# Get users contact info based on role

**URL:** <https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636>\
**Category:** Ignition\
**Created:** [June 27, 2014, 7:09pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636 "2014-06-27T19:09:08Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [June 27, 2014, 7:09pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/1 "2014-06-27T19:09:08Z")

</div>

I have a timer script that sends out an email each week and includes a run time report from an sql query and attaches a file to the email, the recipient addresses are currently being pulled from some tags where I have stored their email addresses.

I have 2 questions;

#1 rather than have the recipients stored in tags how can I pull contact info from ignition users where they belong to a particular role - for example im looking for something like:  
“system.users.get contact info(email address) where role = (supervisor)”

#2 is this weekly email notification im scripting something that is also possible using the notification block? ie. being able to send the notification every Sunday at midnight and include various sql queries and include attachments?

Thank you.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [June 27, 2014, 8:12pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/2 "2014-06-27T20:12:15Z")

</div>

For question #1, I’d take a look at the documentation for [system.user.getUsers()](http://www.inductiveautomation.com/support/usermanuals/ignition/index.html?system_user_getusers.htm). This will return a list of User objects, which have the functions User.getRoles() and User.getContactInfo()

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [June 27, 2014, 10:40pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/3 "2014-06-27T22:40:55Z")

</div>

no problem returning roles… but I cant seem to get anywhere with getContactInfo - keep getting object has no attribute ‘ContactInfo’. can you give an example how syntax for getContactInfo is used?

any advice on returning only users that belong to a certain role?

Thank you.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [June 27, 2014, 11:01pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/4 "2014-06-27T23:01:44Z")

</div>

You can’t get only users with a certain role, but you can select only the users you want from your result. Here’s some Q&D code as an example:

`emails = []
users = system.user.getUsers("")
for user in users:
	if "supervisor" in user.getRoles():
		contacts = user.getContactInfo()
		for contact in contacts:
			if contact.contactType == "email":
				emails.append(contact.value)
for email in emails:
	print email`

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [June 28, 2014, 2:44am UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/5 "2014-06-28T02:44:24Z")

</div>

[quote=“KathyApplebaum”]You can’t get only users with a certain role, but you can select only the users you want from your result. Here’s some Q&D code as an example:

`emails = []
users = system.user.getUsers("")
for user in users:
	if "supervisor" in user.getRoles():
		contacts = user.getContactInfo()
		for contact in contacts:
			if contact.contactType == "email":
				emails.append(contact.value)
for email in emails:
	print email`[/quote]

Ah, got it thank you very much!

using your example this is what I came up with which yields a usable string for email recipients

`import sys
emails = []
users = system.user.getUsers("")
for user in users:
   if "Supervisors" in user.getRoles():
      contacts = user.getContactInfo()
      for contact in contacts:
         if contact.contactType == "email":
            emails.append(contact.value)
emails = repr([x.encode(sys.stdout.encoding) for x in emails]).decode('string-escape')               
system.tag.writeToTag('Email_Recipients',emails)`

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [June 28, 2014, 7:36pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/6 "2014-06-28T19:36:43Z")

</div>

…however the script wont run in a gateway timer script (runs fine in script tester though). the error I get is

`
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Authentication profile "" not found`

any idea how to get this to work?

Thank you.

---

<div class="post-metadata">

**Author:** ![pdibenedetto](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pdibenedetto/32/6364_2.png) [@pdibenedetto](https://forum.inductiveautomation.com/u/pdibenedetto)\
**Post date:** [June 28, 2014, 7:52pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/7 "2014-06-28T19:52:59Z")

</div>

This could be a bug. Acording to [this](http://www.inductiveautomation.com/support/usermanuals/ignition/index.html?system_user_getusers.htm), you must specify the user source in the function getUsers, but if you use “” as argument, the function resolve using the default user source assigned to the project.

Maybe, in the gateway scope, the function can’t resolve what user source is the default.

You can workaround this, hardcoding the user source, for example: system.users.getUsers(“default”), when “default” is your desired user source.

Regards,

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [June 28, 2014, 9:49pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/8 "2014-06-28T21:49:20Z")

</div>

[quote=“pdibenedetto”]This could be a bug. Acording to [this](http://www.inductiveautomation.com/support/usermanuals/ignition/index.html?system_user_getusers.htm), you must specify the user source in the function getUsers, but if you use “” as argument, the function resolve using the default user source assigned to the project.

Maybe, in the gateway scope, the function can’t resolve what user source is the default.

You can workaround this, hardcoding the user source, for example: system.users.getUsers(“default”), when “default” is your desired user source.

Regards,[/quote]

you are correct, thank you. when I define the exact user source the script runs. however now I have found that encode line (second to last) is not executing in the gateway so the email string is returning a “u” before each email address…

---

<div class="post-metadata">

**Author:** ![pdibenedetto](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pdibenedetto/32/6364_2.png) [@pdibenedetto](https://forum.inductiveautomation.com/u/pdibenedetto)\
**Post date:** [June 29, 2014, 1:54am UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/9 "2014-06-29T01:54:58Z")

</div>

I’m not familiar enough with the functions that you use in that line, but, I guess the problem must be that sys.stdout.encoding is returning some different string in gateway context than designer context.

What are you trying to do? Convert the unicode emails strings in ascii format?

If so, you can replace that line with this one:

```auto
emails = [str(email) for email in emails]
```

Or, if you prefer, mantain your line, but replace sys.stdout.econding by ‘ascii’.

I don’t know what you are trying to do with this string (the string representation of the list object) writed in a tag, but If you trying to “save” the list in a tag for later recover, you could mantain unicode format writed in the tag and recover the object using eval function. Some example:

```auto
import sys
emails = []
users = system.user.getUsers("")
for user in users:
   if "Supervisors" in user.getRoles():
      contacts = user.getContactInfo()
      for contact in contacts:
         if contact.contactType == "email":
            emails.append(contact.value)
system.tag.writeToTag('Email_Recipients',emails)

# recover list object in emails2 var from tag value (maintaining unicode format)
emails2 = eval(system.tag.getTagValue('Email_Recipients'))

```

Also, I don’t know what Ignition version are you using, but in newer version it’s recommended to be used system.tag.write and system.tag.read instead of system.tag.writeToTag and system.tag.getTagValue.

Hope this helps.

Regards,

---

<div class="post-metadata">

**Author:** ![LandrethEdward](https://avatars.discourse-cdn.com/v4/letter/l/7bcc69/32.png) [@LandrethEdward](https://forum.inductiveautomation.com/u/LandrethEdward)\
**Post date:** [November 18, 2019, 5:38pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/11 "2019-11-18T17:38:25Z")

</div>

Is there a way to link this method to an on call roster? I tried creating a list of users from system.alarm.getRosters() which worked, but comparing users from the .getUsers isn’t working. Also, I have been unable to pull any of the contact data listed above such as type or value. Thanks!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 19, 2019, 6:56pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/12 "2019-11-19T18:56:04Z")

</div>

Note that there are two examples in there. One if you want to match _any_ role in the roleFilter, and one to match _all_ roles.

```python
rosterUsers = system.alarm.getRosters()['roster_name']
 
roleFilter = set(['Role1', 'Role2', 'Role3'])

userDict = {}

for name in rosterUsers:
	user = system.user.getUser('user_source', name)
	# Test for any role
	if user != None and len(roleFilter.intersection(set(user.getRoles()))) > 0:
	# Test for all roles
	#if user != None and len(roleFilter.intersection(set(user.getRoles()))) == len(roleFilter):
		userDict[name] = user.getContactInfo()

emails = []
for user in userDict:
	for contact in userDict[user]:
		if contact.contactType == 'email':
			emails.append(contact.value)

print emails
```

---

<div class="post-metadata">

**Author:** ![LandrethEdward](https://avatars.discourse-cdn.com/v4/letter/l/7bcc69/32.png) [@LandrethEdward](https://forum.inductiveautomation.com/u/LandrethEdward)\
**Post date:** [November 22, 2019, 4:13pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/13 "2019-11-22T16:13:44Z")

</div>

This worked perfectly and was able to use the emails list in a system.net.sendEmail function. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![prakhar\_vipin\_jain](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@prakhar\_vipin\_jain](https://forum.inductiveautomation.com/u/prakhar_vipin_jain)\
**Post date:** [March 26, 2020, 4:46pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/14 "2020-03-26T16:46:47Z")

</div>

Could you please tell me how were you pulling the email addresses from a tag? I am trying to do that. Thanks!

---

<div class="post-metadata">

**Author:** ![hachjessed](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@hachjessed](https://forum.inductiveautomation.com/u/hachjessed)\
**Post date:** [April 2, 2020, 6:39pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/15 "2020-04-02T18:39:44Z")

</div>

Whats the best way to get a single users email address from their User Management contact info? And output as a string?

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [April 2, 2020, 6:58pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/16 "2020-04-02T18:58:17Z")

</div>

You can do something like:

```auto
userSource = 'Your user source'
userName = 'user name you want contact info from'
data = system.user.getUser(userSource,userName)
conInfo = data.getContactInfo()
for x in range(len(conInfo)):
	i = str(conInfo[x])
	if 'email' in i:
		email = i.split(' ')
		email = email[1]
		print email

```

---

<div class="post-metadata">

**Author:** ![hachjessed](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@hachjessed](https://forum.inductiveautomation.com/u/hachjessed)\
**Post date:** [April 2, 2020, 8:41pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/17 "2020-04-02T20:41:01Z")

</div>

Thanks bpreston. That is a little simpler than what I had originally. Still seems like this should be able to be done with a few less lines.

Here is what I ended up using. Just added in some error handling.

```auto
userSource = 'Your user source'

try: 
	userName = 'user name you want contact info from'
    conInfo = system.user.getUser(userSource ,userName).getContactInfo()
	for x in range(len(conInfo)):
		i = str(conInfo[x])
		if 'email' in i:
			email = i.split(' ')
			email = email[1]
except:
      email = 'Email or user does not exist'

print email

```

---

<div class="post-metadata">

**Author:** ![sharan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sharan](https://forum.inductiveautomation.com/u/sharan)\
**Post date:** [January 17, 2022, 4:46pm UTC](https://forum.inductiveautomation.com/t/get-users-contact-info-based-on-role/8636/18 "2022-01-17T16:46:54Z")

</div>

Above code worked fine, I wanted just phone numbers of all users, so modified the code accordingly…

import sys  
email = []  
users = system.user.getUsers("")  
for user in users :

```
   contacts = user.getContactInfo()
    for contact in contacts:
        if contact.contactType == "phone":
          email.append(contact.value)

```

Number = email  
Number1 = Number[0]  
Number2 = Number[1]  
Number3 = Number[2]  
Number4 = Number[3]  
Number5 = Number[4]
