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.

if (portalItem.getAccess() == PortalItem.Access.PRIVATE) {

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 shareWithAsync method and pass true as the everyone argument.

//Share publicly, with everyone
final ListenableFuture<Void> shareFuture = portalItem.shareWithAsync(true, false);
shareFuture.addDoneListener(() -> {
  try {
    shareFuture.get();
    System.out.println("Shared item with everyone");
  } catch (InterruptedException | ExecutionException e) {
    // ... deal iwth exception
  }
});

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 shareWithAsync method.

//Share with just the user's organization
final ListenableFuture<Void> shareFuture = portalItem.shareWithAsync(false, true);
shareFuture.addDoneListener(() -> {
  try {
    shareFuture.get();
    System.out.println("Shared item with the organization");
  } catch (InterruptedException | ExecutionException e) {
    // ... deal with exception
  }
});

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 fetchGroupsAsync 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.

// Get a list of all the groups the item is shared with
final ListenableFuture<PortalItemGroups> itemGroupsFuture = portalItem.fetchGroupsAsync();
itemGroupsFuture.addDoneListener(() -> {
  try {
    PortalItemGroups portalItemGroups = itemGroupsFuture.get();


    // Build a message with the groups that this portal item belongs to
    StringBuilder messageBuilder =
      new StringBuilder(String.format("'%s' is a member of the following groups:\n", portalItem.getTitle()));
    List<PortalGroup> adminGroups = portalItemGroups.getAdmins();
    for (PortalGroup adminGroup : adminGroups) {
      messageBuilder.append(adminGroup.getTitle()).append("\n");
    }
    List<PortalGroup> memberGroups = portalItemGroups.getMembers();
    for (PortalGroup memberGroup : memberGroups) {
      messageBuilder.append(memberGroup.getTitle()).append("\n");
    }
    List<PortalGroup> otherGroups = portalItemGroups.getOthers();
    for (PortalGroup otherGroup : otherGroups) {
      messageBuilder.append(otherGroup.getTitle()).append("\n");
    }
    System.out.println(messageBuilder.toString());


  } catch (InterruptedException | ExecutionException e) {
    // ... deal with exception
  }
});

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 PortalUser.getGroups() as follows:

// Get all the users groups
List<PortalGroup> usersGroups = authenticatedUser.getGroups();
// Decide which groups to share with
final ArrayList<PortalGroup> shareWithGroups = new ArrayList<>();
for (PortalGroup group : usersGroups) {
  // Only choose groups that are not view-only
  if (!group.isViewOnly()) {
    shareWithGroups.add(group);
  }
}

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 shareWithGroupsAsync method as follows:

final ListenableFuture<List<PortalGroup>> shareFuture = portalItemToShare.shareWithGroupsAsync(shareWithGroups);
shareFuture.addDoneListener(() -> {
  try {
    // Get a list of any portal groups which the item failed to be shared into.
    List<PortalGroup> failedGroups = shareFuture.get();
    if ((failedGroups != null) && (failedGroups.size() > 0)) {
      System.out.println("Sharing failed for " + failedGroups.size() + " of the groups:");
    } else {
      System.out.println(String.format("Item shared into %d groups", shareWithGroups.size()));
    }
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
  }
});

If there is a problem sharing the portal item with some groups, the failures will be indicated in the values returned from the shareWithGroupsAsync method.

Unshare a portal item from specific groups

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

// Unshare the item from the specified list of groups
final ListenableFuture<List<PortalGroup>> unshareFuture = portalItemToShare.unshareGroupsAsync(unshareGroups);
unshareFuture.addDoneListener(() -> {
  try {
    // Get a list of any portal groups which the item unshare failed, for example groups it wasnt already shared with
    List<PortalGroup> failedGroups = unshareFuture.get();
    if ((failedGroups != null) && (failedGroups.size() > 0)) {
      System.out.println("Unsharing failed for " + failedGroups.size() + " of the groups:");
    } else {
      System.out.println(String.format("Item unshared from %d groups", unshareGroups.size()));
    }
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
  }
});

Stop sharing

To stop sharing the item, call the unshareAsync 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

// Make the portalitem private
final ListenableFuture<Void> unshareFuture = portalItemToShare.unshareAsync();
unshareFuture.addDoneListener(() -> {
  try {
    unshareFuture.get();
    System.out.println("Stopped sharing portal item");
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
  }
});