Granting Access to a Group
Problem
You need to grant a group access to a content item.
Solution
Use the Python or R SDKs to grant access. You need to specify the following information:
- The
content_guid
for the content item from which you want to add permissions. - Either the
group_guid
orgroup_name
for the group being granted access. Thegroup_name
is used ifgroup_guid
is blank. - The
access_type
which determines the type of permissions the group receives. Valid values are “viewer” and “owner” (collaborator).
from posit import connect
# 1. specify the guid for the content item
= "CONTENT_GUID_HERE"
content_guid # 2. specify either the guid or name for the group to be added (group_name will be used if group_guid is blank)
= ""
group_guid = "GROUP_NAME_HERE"
group_name # 3. specify if the group should be added as a "viewer" or "owner" (collaborator)
= "viewer"
access_type
= connect.Client()
client
# search by group_name to find the group_guid if blank
if not group_guid and group_name:
= client.get("/v1/groups", params={"prefix": group_name}).json()
group_match if not group_match["results"]:
raise Exception("Invalid group name")
elif len(group_match["results"]) != 1:
raise Exception("More than one group name found, ensure you enter a unique name")
else:
= group_match["results"][0]["guid"]
group_guid elif not group_name:
raise Exception("Either group_guid or group_name must be specified")
# For the specified content item add the desired group
client.content.get(content_guid).permissions.create(=group_guid,
principal_guid="group",
principal_type=access_type,
role
)
# Confirm new permissions
client.content.get(content_guid).permissions.find()
library(connectapi)
# 1. specify the guid for the content item
= "CONTENT_GUID_HERE"
content_guid # 2. specify either the guid or name for the group to be added (group_name will be used if group_guid is blank)
= ""
group_guid = "GROUP_NAME_HERE"
group_name # 3. specify if the group should be added as a "viewer" or "owner" (collaborator)
= "viewer"
access_type
<- connect()
client
# search by group_name to find the group_guid if blank
if (group_guid == "" && group_name != "") {
<- get_groups(client, prefix = group_name)
group_match if (nrow(group_match) == 0) {
stop("Invalid group name")
else if (length(unique(group_match$name)) != 1) {
} stop("More than one group name found, ensure you enter a unique name")
else {
} <- unique(group_match$guid)
group_guid
}else if (group_name == "") {
} stop("Either group_guid or group_name must be specified")
}
# For the specified content item add the desired group
<- content_item(client, content_guid)
content content_add_group(content, group_guid, role = access_type)
# Confirm new permissions
get_content_permissions(content)