Skip To Content

Access portal content

In this topic

Portals such as ArcGIS Online and Portal for ArcGIS can store many types of data. Perhaps the type most commonly used by apps is a web map—a JSON description of the data in a map, along with other display and behavioral properties. Other types include tile packages, feature collections, and services. Some types, such as globe documents and map templates, may only be relevant to desktop systems. Although you can access these items using the API, you may not be able to use them on a mobile device.

The ArcGISPortalItem object represents an individual item stored on a portal. Each item includes the following:

  • Metadata, such as a unique identifier (ID), title, summary, description, thumbnail image, and tags
  • Binary or text-based data—for example, the JSON that defines a web map or a feature collection, or a URL pointing to a map service, or the binary data of a tile package or shapefile

Web page showing properties of a portal item including the item ID

Apps use portal items in different ways. They may show a user a range of items from a portal by combining the title, thumbnail image, and other metadata. This approach is often used to allow users to browse web maps, basemaps, or map services, and identify the item to work with. If a user selects an item, the app may then download the data stored in the portal item, for example, by opening and displaying a web map in a map, or downloading a tile package. Apps sometimes access items directly by ID if a specific item should always be used.

Your app can also add new portal items and alter existing items by adding comments and ratings, updating item information, sharing, unsharing, and deleting them, as long as the authenticated user has access permissions to do so.

You access the content of a portal item differently depending on its type. Below you can find examples of accessing different types of portal items. For more information on connecting to a portal, see Access the ArcGIS platform. To find out how you can allow apps to browse or search for items and groups, see Search portal content.

Display a web map by ID

A very common task is to display a web map from a portal. Web maps are stored as portal items and can be opened by using the ID of the item, along with the URL of the portal. If the map is not public (for example, if it is shared only with a specific group), you also need to pass in valid credentials with permission to view the map. See Access the ArcGIS platform for more information on accessing secured items.

In the following example, the web map is opened using the WebMap.FromPortalItemAsync static method and is loaded into a WebMapViewModel object. The WebMapViewModel class is designed to work as a view model for apps designed using the Model-View-ViewModel (MVVM) pattern. Changes made to the web map saved in the portal will be reflected in the map displayed in your app's map view. For more information about using the MVVM pattern with the ArcGIS Runtime SDK for .NET, see the Use the MVVM design pattern tutorial.

// get a reference to the portal (ArcGIS Online)
var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync();

// get a portal item using its ID (ArcGISWebException is thrown if the item is not found)
var item = await Esri.ArcGISRuntime.Portal.ArcGISPortalItem.CreateAsync(portal, "16055a012a4c4bfdb972c90e20b5e7b8");

// check type: if the item is not a web map, return
if (item.Type != Esri.ArcGISRuntime.Portal.ItemType.WebMap) { return; }

// get the web map represented by the portal item
var webMap = await Esri.ArcGISRuntime.WebMap.WebMap.FromPortalItemAsync(item);

// create a WebMapViewModel and load the web map
var webMapVM = await Esri.ArcGISRuntime.WebMap.WebMapViewModel.LoadAsync(webMap, portal);

// get the map from the view model; display it in the app's map view
this.MyMapView.Map = webMapVM.Map;

Open a web map

You can also create an object that represents a web map without needing to display it immediately. From this, you could find out more about the contents of the map before displaying it—for example, getting the basemap, operational layers, bookmarks, and initial extent.

The following example gets a web map portal item, finds one of its bookmarks, and then displays it in the app's map view control and zooms to the extent of the bookmark.

// get a reference to the portal (ArcGIS Online)
var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync();

// get a portal item using its ID (ArcGISWebException is thrown if the item is not found)
var item = await Esri.ArcGISRuntime.Portal.ArcGISPortalItem.CreateAsync(portal, "16055a012a4c4bfdb972c90e20b5e7b8");

// check type: if the item is not a web map, return
if (item.Type != Esri.ArcGISRuntime.Portal.ItemType.WebMap) { return; }

// get the web map represented by the portal item
var webMap = await Esri.ArcGISRuntime.WebMap.WebMap.FromPortalItemAsync(item);

// get the "Chula Vista" bookmark
var bookMark = webMap.Bookmarks.Where(b => b.Name == "Chula Vista").FirstOrDefault();

// create a WebMapViewModel and load the web map
var webMapVM = await Esri.ArcGISRuntime.WebMap.WebMapViewModel.LoadAsync(webMap, portal);

// get the map from the view model; display it in the app's map view
this.MyMapView.Map = webMapVM.Map;

// zoom to the extent of the bookmark
this.MyMapView.SetViewAsync(bookMark.Extent);

Note:

Remember that the credentials must be for a valid user with access to the item.

Create a layer from a service item by ID

Portal items representing map or feature services can be used to create layers by using the portal item URL. The following example gets a portal item that represents a feature service and reads the URL. The first layer in the service is added to the map.

// get a reference to the portal (ArcGIS Online)
var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync();

// get a portal item using its ID (ArcGISWebException is thrown if the item is not found)
var item = await Esri.ArcGISRuntime.Portal.ArcGISPortalItem.CreateAsync(portal, "9fb4d96077e24bcba72ba6cf1139c9cf");

// check type: if the item is not a feature service, return
if (item.Type != Esri.ArcGISRuntime.Portal.ItemType.FeatureService) { return; }

// build a url to the first layer in the service
var uri = new Uri(item.Url + "/0");
// create a new feature layer using the url
var featureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer(uri);
// add the layer to the map
this.MyMapView.Map.Layers.Add(featureLayer);

Find information about any portal item by ID

You can use the ID of any item stored in a portal to access it, and also to find its fields—properties such as its title, description, thumbnail image, owner, and any ratings or comments added by portal users. You can also find out the type of data it contains.

// get a reference to the portal (ArcGIS Online)
var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync();

// get a portal item using its ID (ArcGISWebException is thrown if the item is not found)
var item = await Esri.ArcGISRuntime.Portal.ArcGISPortalItem.CreateAsync(portal, "9fb4d96077e24bcba72ba6cf1139c9cf");

// get some information about this item
var title = item.Title;
var createdOn = item.CreationDate;
var viewed = item.NumViews;

var itemInfo = string.Format("{0} was created on {1} and has been viewed {2} times.", title, createdOn, viewed);

Note:

The thumbnail of a portal item, along with the ratings and data, is not returned from the portal when you initially create the item, in order to reduce the network traffic and memory requirements. See the section below on how to access these properties.

Fetch thumbnails of items

When you create a portal item object, not all information associated with it is immediately returned. This allows you to work with the item using a minimum amount of memory and delay fetching the larger pieces of data unless, or until, you really need them. Information you need to fetch, if required, includes thumbnail images, ratings and comments, item data, group memberships, and user folder contents.

The following example shows how to create a bitmap image to display a portal item thumbnail.

// get a reference to the portal (ArcGIS Online)
var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync();

// get a portal item using its ID (ArcGISWebException is thrown if the item is not found)
var item = await Esri.ArcGISRuntime.Portal.ArcGISPortalItem.CreateAsync(portal, "9fb4d96077e24bcba72ba6cf1139c9cf");

// create a new bitmap from the item thumbnail uri
var uri = item.ThumbnailUri;
var bitmap = new BitmapImage(uri);
this.itemThumbailImage.Source = bitmap;

The same applies to the thumbnails of other classes, like users, groups, and organizations. ArcGISPortalUser, ArcGISPortalGroup, and ArcGISPortalItem all provide a ThumbnailUri property.

Access item data

Portal items that represent files, such as tile packages, images, documents, PDF files, and so on, can be opened, downloaded, and then stored or opened in your app or in another app. As in the previous example, you will need to specifically fetch the data of a portal item after you create it. Use the GetItemDataAsync method to do this.

The following example gets the data for an image (.jpg) stored in the portal and saves it to a local file.

try
{
    // get a reference to the portal (ArcGIS Online)
    var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync();

    // get a portal item using its ID
    var item = await Esri.ArcGISRuntime.Portal.ArcGISPortalItem.CreateAsync(portal, "9f4a793bf45d4292a820918524febfef");

    // check type: if the item is not an image, return
    if (item.Type != Esri.ArcGISRuntime.Portal.ItemType.Image) { return; }

    // have the user choose a new output file for the image
    var fileDialog = new Microsoft.Win32.SaveFileDialog();
    fileDialog.FileName = item.Name;
    fileDialog.DefaultExt = ".jpg";
    fileDialog.Filter = "JPEG images|*.jpg";
    var ok = fileDialog.ShowDialog();
    if (ok != true) { return; }

    // create the output file
    var outFile = System.IO.File.OpenWrite(outFileName);

    // get the data for the item
    var stream = await item.GetItemDataAsync();

    // read the data from the stream and write it to the output file
    var reader = new System.IO.BinaryReader(stream);
    using (var writer = new System.IO.BinaryWriter(outFile))
    {
        writer.Write(reader.ReadBytes((int)stream.Length));
        writer.Flush();
        reader.Close();
    }

    MessageBox.Show("Saved item to " + outFileName);
}
catch (Esri.ArcGISRuntime.Http.ArcGISWebException webExp)
{
    MessageBox.Show("Unable to access portal item: " + webExp.Message);
}
catch (Exception exp)
{
    MessageBox.Show("Unable to save file: " + exp.Message);
}

Fetch a user's content

Many apps present a list of maps or other items that belong to the current user. Users can create folders within their portal accounts to help organize their work. Apps should respect the user's view of their content and present the list of folders to the user, allowing the user to choose which folder to look at in detail.

It's easy to get all the portal items owned by the authenticated user (that represent the content and services items created by that user).

The following example connects to ArcGIS Online with a user name and password, and then gets the groups and items belonging to the authenticated user.

try
{
    // create credential from input username and password
    // (exception will be thrown here if login fails)
    var creds = await IdentityManager.Current.GenerateCredentialAsync(
        "https://www.arcgis.com/sharing/rest", 
        this.UsernameTextBox.Text, 
        this.PasswordTextBox.Password);

    // add credential to the IdentityManager
    IdentityManager.Current.AddCredential(creds);

    // get the portal, the connection will be made using the credentials supplied above
    var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync(
        new Uri("https://www.arcgis.com/sharing/rest"));

    // get the portal user (null if connecting anonymously)
    var user = portal.CurrentUser;

    // get a list of items and groups for this user
    if (user != null)
    {
        var items = await user.GetItemsAsync();
        var groups = await user.GetGroupsAsync();

        // work with items and groups here ...
    }
}
catch (Exception exp)
{
    throw new Exception("Unable to connect to portal: " + exp.Message);
}

Note:

As seen in the code above, user content is another example of information that you must explicitly fetch, like portal item thumbnails and content.

To get items shared with the current user, or items owned by a different user, you need to search portal content.

List the groups a user belongs to

Groups can be configured in different ways—some groups are open to all users; some are only accessible by invitation; and for others, any user can request to join. The groups to which the authenticated user belongs can be accessed, and for any group, the identities of the members can be found.

From the group, you can find out if membership is by invitation only (using TODO), and if the user can share items with the group (using ArcGISPortalGroup.IsInvitationOnly). To find out the items currently shared with a specific group, you need to use the group ID to search for content in a group. To find other groups, for example, those relating to a keyword (tag), you can search for groups, or find the featured groups of a portal.

Samples

You'll find a complete portal viewer app that illustrates working with portal content in the arcgis-portalviewer-dotnet github repository.

Related topics