Share a portal item

When one of your users creates a portal item and adds it to a portal, using ArcGIS Online or this ArcGIS Runtime API, the item is private by default. Only the owner of the item (and the organization's administrators) are able to view it. This level of sharing is indicated by the portal item's access property.

switch (m_portalItem->access())
{
case PortalAccess::Organization:
  qDebug() << "Organization access";
  break;
case PortalAccess::Private:
  qDebug() << "Private access";
  break;
case PortalAccess::Public:
  qDebug() << "Public access";
  break;
case PortalAccess::Shared:
  qDebug() << "Shared access";
  break;
case PortalAccess::Unknown:
default:
  qDebug() << "Unknown access";
  break;
}

The access property indicates the item's highest level of sharing. So if the portal item has an access value of:

  • Private—The item is not be shared with anybody. Only the owner and administrator can view the item.
  • Public—The item can be viewed by everybody.
  • Organization—The item can be viewed by every member of the organization that the item is created in.
  • Shared—The item can only be viewed by members of a group that the item is shared with.

You can allow users to change this level of sharing so that the portal item is available to the right users.

Share with everyone or a portal's organization

If you want your portal item to be accessible to everyone, you need to make it public. Any user who has access to the portal website can then find and use the portal item. To make a portal item public, call the shareWith method and pass true for the everyone argument.

bool everyone = true;
bool organization = false;
m_portalItem->shareWith(everyone, organization);
connect(m_portalItem, &PortalItem::shareWithCompleted, this, [this](bool success)
{
  if (!success)
    return;


  switch (m_portalItem->access())
  {
  case PortalAccess::Public:
    qDebug() << "shared with everyone";
    break;
  case PortalAccess::Organization:
    qDebug() << "shared with organization";
    break;
  default:
    break;
  }
});

To ensure only members of the user's portal have access to an item, you can share it with just the user's portal organization. To do this, pass true for the organization parameter of the shareWith method.

everyone = false;
organization = true;
m_portalItem->shareWith(everyone, organization);
connect(m_portalItem, &PortalItem::shareWithCompleted, this, [this](bool success)
{
  if (!success)
    return;


  switch (m_portalItem->access())
  {
  case PortalAccess::Public:
    qDebug() << "shared with everyone";
    break;
  case PortalAccess::Organization:
    qDebug() << "shared with organization";
    break;
  default:
    break;
  }
});

Share with a portal group

If a user is a member of a group, they can share items that they own with that group. Sharing items with specific groups restricts access to a smaller, more focused set of people. For details on portal groups, seeWhat is a group?. The following sections describe how to:

  • Find the groups that the portal item belongs to
  • Find the groups the user is a member of
  • Share a portal item with a group
  • Unshared a portal item from a group

Find a portal item's groups

You can determine which groups a portal item is currently shared with by calling the fetchGroups method on the portal item. This asynchronous method returns three types of groups. In this example:

  • adminGroups contains the groups which the current user is an administrator of.
  • memberGroups contains the groups in which the user is a member.
  • otherGroups contains the public groups or groups that are shared to the user's organization but that the user is not a member of.

This code displays the title of the groups that the item is shared with.

int adminGroupsCount = 0;
int memberGroupsCount = 0;
int otherGroupsCount = 0;
connect(portalItem, &PortalItem::fetchGroupsCompleted,
        this, [portalItem, &adminGroupsCount, &memberGroupsCount, &otherGroupsCount](bool success)
{
  if (!success)
    return;


  // lambda function to iterate over PortalGroupListModels
  auto printGroupTitles = [portalItem](PortalGroupListModel* groupsModel, const QString& groupName)
  {
    if (!groupsModel)
      return;


    qDebug() << groupName << ":";
    for (int i = 0; i < groupsModel->rowCount(); ++i)
    {
      if (groupsModel->at(i))
        qDebug() << groupsModel->at(i)->title();
    }
  };


  printGroupTitles(portalItem->adminGroups(), "admin groups");
  printGroupTitles(portalItem->memberGroups(), "member groups");
  printGroupTitles(portalItem->otherGroups(), "other groups");
});


portalItem->fetchGroups();

Share a portal item with specific groups

To share a portal item with specific groups, first determine which groups it should be shared with. For example, you can determine which groups the current user is a member of by iterating through the groups returned from the PortalGroupListModel obtained by calling groups on a portal user as follows:

qDebug() << "User's groups:";
PortalGroupListModel* groups = portalUser.groups();
Q_CHECK_PTR(groups);


for (int i = 0; i < groups->size(); ++i)
{
  Q_CHECK_PTR(groups->at(i));
  qDebug() << groups->at(i)->title();
};

Note that some groups are view-only; that is, members can view the items already in the group, but cannot share other items with that group. To learn more about users and groups, see Access content and Access the ArcGIS Platform.

Then to share the portal item with those groups, pass a list of these groups to the shareWithGroups method as follows:

PortalGroupListModel* userGroups = m_portal->portalUser()->groups();
QStringList groupIds { "wrongGroupId" };
for (int i = 0; i < userGroups->rowCount(); ++i)
{
  if (userGroups->at(i))
    groupIds.append(userGroups->at(i)->groupId());
}


connect(m_portalItem, &PortalItem::shareWithGroupsCompleted, this, [this](bool success, const QStringList& groupIds)
{
  VERIFY(success);
  COMPARE(groupIds, { "wrongGroupId" });
  COMPARE(m_portalItem->access(), PortalAccess::Shared);
});


m_portalItem->shareWithGroups(groupIds);

If there is a problem sharing the portal item with some groups, the failures will be indicated in the values returned from the shareWithGroupsCompleted signal, which is emitted upon completion of the shareWithGroups method.

Unshare a portal item from specific groups

To stop sharing a portal item with specific groups pass these groups to the method as follows:

connect(m_portalItem, &PortalItem::unshareGroupsCompleted, this, [this](bool success, const QStringList& groupIds)
{
  VERIFY(success);
  COMPARE(groupIds, { "wrongGroupId" });
  COMPARE(m_portalItem->access(), PortalAccess::Private);
});


m_portalItem->unshareGroups(groupsIds);

Stop sharing

To stop sharing the item, call the unshare method. Calling this method:

  • Makes the item private and accessible to only the item owner (and the organization's administrator)
  • Removes all groups from the item's groups list

connect(m_portalItem, &PortalItem::unshareCompleted, this, [this](bool success)
{
  if (!success)
    return;


  switch (m_portalItem->access())
  {
  case PortalAccess::Private:
    qDebug() << "unshared";
    break;
  default:
    break;
  }
});


m_portalItem->unshare();