Skip to content

Instantly share code, notes, and snippets.

@oevans
Last active December 19, 2015 14:08
Show Gist options
  • Select an option

  • Save oevans/5966643 to your computer and use it in GitHub Desktop.

Select an option

Save oevans/5966643 to your computer and use it in GitHub Desktop.
## Admin script for adding new users (those that recently accepted invitations) to a group or groups.
## This script uses Evan Caldwell's agolTools module available on GitHub here: https://github.com/Esri/ago-tools
import csv, time, urllib, json, datetime
from datetime import date, timedelta
from agolTools import admin
myAgol = admin.admin()
users = myAgol.getUsers()
## --- UPDATE THESE VARIABLES WITH YOUR INFORMATION ---
# Location/name for output report file
# Provide your outputDir and outputFile (outputDate does not need to be modified)
outputDir = r'Z:\Documents\Esri\Python\Output\'
outputDate = datetime.datetime.now().strftime("%Y%m%d")
outputFile = outputDir + outputDate + '_AddNewUsers2Groups.csv'
# Script parameters
daysToCheck = 2 #e.g., 1 will check past day, 7 will check past week, etc.
groups = ['d93aabd856f8459a8905a5bd434d4d4a', 'f84c841a3dfc4591b1ff83281ea5025f'] #provide one or more group IDs as strings (in quotes) separated by commas
## Create a list of all new users (joined in the last 'daysToCheck' days)
newUsers = []
for user in users:
if date.fromtimestamp(float(user['created'])/1000) > date.today()-timedelta(days=daysToCheck):
newUsers.append(user)
## Assign new users to group(s) specified above
parameters = urllib.urlencode({'token' : myAgol.token, 'f' : 'pjson'})
for groupID in groups:
for newUser in newUsers:
user = newUser['username']
print 'Attempting to add ' + user + ' to groupID ' + groupID
# Add Users - REQUIRES POST method
response = urllib.urlopen(myAgol.portalUrl + '/sharing/rest/community/groups/' + groupID + '/addUsers?', 'users=' + user + "&" + parameters).read()
print response
# Error checking - print to console
if not 'success' in json.loads(response):
print 'ERROR: ' + user + ' was NOT added to groupID ' + groupID
## Create output report
with open(outputFile, 'wb') as outputFile:
dataWriter = csv.writer(outputFile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
# Write header row.
dataWriter.writerow(['Full Name', 'Email', 'Username', 'Role', 'Date Created'])
# Write user data.
for newUser in newUsers:
dataWriter.writerow([newUser['fullName'], newUser['email'], newUser['username'], newUser['role'],
time.strftime("%Y-%m-%d", time.gmtime(newUser['created']/1000))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment