Learn how to use the portal service to create a new group in your portal, and add members to the group.
Prerequisites
You need an ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account to use your portal.
Steps
Get the portal URL
To access a portal, you need an ArcGIS account which is associated with an organization that allows you to store and manage your content.
- In a web browser, sign in to your portal with your ArcGIS account.
- Identify the portal URL from the navigation bar. The base URL should be one of the following:
- ArcGIS Location Platform:
https:
//www.arcgis.com/sharing/rest - ArcGIS Online:
https:
//www.arcgis.com/sharing/rest - ArcGIS Enterprise:
https:
//{machine.domain.com}/{webadaptor}/rest
- ArcGIS Location Platform:
Get an access token
To perform the portal service operation in this tutorial, you need an access token from OAuth 2.0 credentials. It is the only supported form of authentication. The use of API keys is not supported.
-
Go to the Create OAuth credentials for user authentication tutorial to get a set of OAuth 2.0 credentials.
-
Get an access token from the OAuth 2.0 credentials.
-
Copy the access token to your clipboard when prompted.
Create a new group and add members
-
Add the following import statement to your code.
main.pyUse dark colors for code blocks from arcgis.gis import GIS
-
Establish a connection to the portal using the
GIS()
module.main.pyUse dark colors for code blocks from arcgis.gis import GIS # Authenticate with your ArcGIS Online account gis = GIS("https://www.arcgis.com", "your_username", "your_password")
-
Create a dictionary of the new group and add it using the
create_
method.from_ dict main.pyUse dark colors for code blocks from arcgis.gis import GIS # Authenticate with your ArcGIS Online account gis = GIS("https://www.arcgis.com", "your_username", "your_password") # Create a group group_properties = { "title": "Example Group", "description": "This is an example group", "tags": "example, group, arcgis python", "snippet": "Example group", "access": "public" # Can be 'private', 'org', or 'public' } group = gis.groups.create_from_dict(group_properties) print(f"Group created: {group.title}")
-
Add existing members to the new group using the
add_
method.users main.pyUse dark colors for code blocks from arcgis.gis import GIS # Authenticate with your ArcGIS Online account gis = GIS("https://www.arcgis.com", "your_username", "your_password") # Create a group group_properties = { "title": "Example Group", "description": "This is an example group", "tags": "example, group, arcgis python", "snippet": "Example group", "access": "public" # Can be 'private', 'org', or 'public' } group = gis.groups.create_from_dict(group_properties) print(f"Group created: {group.title}") # Add an existing member to the group usernames_to_add = ["member_username1", "member_username2"] result = group.add_users(usernames_to_add)
-
Verify members were added to the group.
main.pyUse dark colors for code blocks from arcgis.gis import GIS # Authenticate with your ArcGIS Online account gis = GIS("https://www.arcgis.com", "your_username", "your_password") # Create a group group_properties = { "title": "Example Group", "description": "This is an example group", "tags": "example, group, arcgis python", "snippet": "Example group", "access": "public" # Can be 'private', 'org', or 'public' } group = gis.groups.create_from_dict(group_properties) print(f"Group created: {group.title}") # Add an existing member to the group usernames_to_add = ["member_username1", "member_username2"] result = group.add_users(usernames_to_add) if 'notAdded' in result and result['notAdded']: print(f"Failed to add user(s): {result['notAdded']}") else: print(f"Users added successfully to the group {group.title}")
-
Add the following import statement to your code.
rest-js.jsUse dark colors for code blocks import { ApiKeyManager } from '@esri/arcgis-rest-request'; import { createGroup, addGroupUsers } from '@esri/arcgis-rest-portal';
-
Create a user session using your credentials
rest-js.jsUse dark colors for code blocks import { ApiKeyManager } from '@esri/arcgis-rest-request'; import { createGroup, addGroupUsers } from '@esri/arcgis-rest-portal'; const apiKey = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(apiKey);
-
Create a new group
rest-js.jsUse dark colors for code blocks import { ApiKeyManager } from '@esri/arcgis-rest-request'; import { createGroup, addGroupUsers } from '@esri/arcgis-rest-portal'; const apiKey = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(apiKey); // Create a new group const createGroupResponse = await createGroup({ title: 'New Group Title', description: 'Description of the new group', access: 'public', // other options are 'org' and 'private' authentication, });
-
Add exiting members to the new group
rest-js.jsUse dark colors for code blocks import { ApiKeyManager } from '@esri/arcgis-rest-request'; import { createGroup, addGroupUsers } from '@esri/arcgis-rest-portal'; const apiKey = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(apiKey); // Create a new group const createGroupResponse = await createGroup({ title: 'New Group Title', description: 'Description of the new group', access: 'public', // other options are 'org' and 'private' authentication, }); if (createGroupResponse.success) { const groupId = createGroupResponse.group.id; console.log(`Group created successfully with ID: ${groupId}`); // Add existing members to the newly created group const usernameToAdd = ['existing_member_username1', 'existing_member_username2']; const addGroupUsersResponse = await addGroupUsers({ id: groupId, users: usernameToAdd, authentication, });
-
Verify members was added to the group
rest-js.jsUse dark colors for code blocks import { ApiKeyManager } from '@esri/arcgis-rest-request'; import { createGroup, addGroupUsers } from '@esri/arcgis-rest-portal'; const apiKey = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(apiKey); // Create a new group const createGroupResponse = await createGroup({ title: 'New Group Title', description: 'Description of the new group', access: 'public', // other options are 'org' and 'private' authentication, }); if (createGroupResponse.success) { const groupId = createGroupResponse.group.id; console.log(`Group created successfully with ID: ${groupId}`); // Add existing members to the newly created group const usernameToAdd = ['existing_member_username1', 'existing_member_username2']; const addGroupUsersResponse = await addGroupUsers({ id: groupId, users: usernameToAdd, authentication, }); if (addGroupUsersResponse.notAdded.length === 0) { console.log(`Users added to the group successfully.`); } else { console.log(`Failed to add users to the group.`); console.log('Details:', addGroupUsersResponse.notAdded); }
View the results
If successful, your code will print an output like this to verify that the operation was successful:
Group created: Example Group
Users added successfully to the group Example Group
Group created successfully with ID: <group_id>
Users added to the group successfully.
What's next
![](/documentation/static/access-feature-layer-data-fe6f3f2d53d5ce5d3207e3b43c9c6c59.png)
Sign in and access your portal
Use the portal service to sign in and access your portal.
![](/documentation/static/access-feature-layer-data-fe6f3f2d53d5ce5d3207e3b43c9c6c59.png)
List items in your portal
Use the portal service to list items in your portal.
![](/documentation/static/access-feature-layer-data-fe6f3f2d53d5ce5d3207e3b43c9c6c59.png)
Set sharing level for an item
Use the portal service to set the sharing level for an item in your portal.