Managing your portal or AGOL site can be a tough task, and ArcREST is here to help. Often within your organization, individuals need new groups or maybe you need to create a set of default groups. ArcREST allows individuals to create a list of group then put users into that group easily.
The Pointy Hair Boss (http://www.andrewlipson.com/)
So here is the situation. You boss wants you to create a group called 'Publishing Review'. This group will contain all your users must be in this group. The point of this group is that non-administrator accounts cannot publish to the public, and any data within this group will be reviewed by administrators on a given cycle and published if the information meets the organizations standards.
Let's begin!
import arcrest
#--------------------------------------------------------------------------
def getAllUsers(portalId, admin):
"""
returns all the users for a given AGOL
Inputs:
portalId - unique id of the portal
admin - manageorg.Administration object
Output:
returns a list of the users
"""
start = 1
num = 100
portals = admin.portals(portalId=portalId)
count = 0
nextStart = 0
results = []
while nextStart > -1:
users = portals.users(start=start + (num * count),
num=num)
results = results + users['users']
count += 1
nextStart = users['nextStart']
del users
return results
#--------------------------------------------------------------------------
if __name__ == "__main__":
# Connect to the site
#
sh = arcrest.AGOLTokenSecurityHandler(username, password)
admin = arcrest.manageorg.Administration(securityHandler=sh,
initialize=True)
community = admin.community
portals = admin.portals()
groups = community.groups
portalId = portals.portalId
# Create Group
# Basic group inputs
access = "org"
groupTitle = "Pre Publishing Group"
groupTags = "Publishing;Service Management"
description = "This group allows users to request publishing services to public."
# search to see if the group exists. If not, create the group, else use the ID
searchResult = community.getGroupIDs(groupNames=groupTitle)
if len(searchResult) == 0:
groupId = community.createGroup(title=groupTitle,
tags=groupTags,
description=description,
snippet="",
phone="",
access=access,
sortField="title",
sortOrder="asc",
isViewOnly=False,
isInvitationOnly=False,
thumbnail=None)['group']['id']
else:
groupId = searchResult[0]
# Get all the site's users and add them to the group
users = [user['username'] for user in getAllUsers(portalId=portalId, admin=admin)]
if len(users) > 0:
groups.addUsersToGroups(users=",".join(users), groupID=groupId)
So let's review the code. First we connect to our site. Next we grab all the objects we will need to work with in order to make the magic happen. Since the topic is groups, there is a need to work with the community functions as well as some portal functions. This will give us all the access we need. After we do all of that, we need to know the group ID, this is found by either creating the group or if it exists, getting the site to return the information to us. Using the community object, call the getGroupIDs() returns the IDs for a group or groups as a list. If the list if < 1 then create the group using the createGroup() else use the search result Id to add the users.
To get all the users, I created a function that will go and grab all the users from the portal object. All you need to pass is the portalId and the administration objects and I detailed list of users will be returned. Since in this sample we only need username, we can just parse out what we need and move on as shown above.
Hope this have been a helpful post, keep on rocking, and post your questions/comments/enhancements on the ArcREST page: http://www.github.com/Esri/ArcREST