ArcGIS Pro 3.6 API Reference Guide
ArcGIS.Desktop.Core Namespace / ArcGISPortalExtensions Class / GetUserContentAsync Method
the username
optional folderid
Example

In This Topic
    GetUserContentAsync Method
    In This Topic
    Gets the given username's content. Items are either in the home folder for the user, e.g. /content/users/{username} or in a subfolder of the home folder with the given folder ID. Multilevel folders are not supported.
    Syntax
    public static Task<PortalUserContent> GetUserContentAsync( 
       ArcGISPortal portal,
       string username,
       string folderId
    )
    Public Shared Function GetUserContentAsync( _
       ByVal portal As ArcGISPortal, _
       ByVal username As String, _
       Optional ByVal folderId As String _
    ) As Task(Of PortalUserContent)

    Parameters

    portal
    username
    the username
    folderId
    optional folderid

    Return Value

    Exceptions
    ExceptionDescription
    Thrown when an invalid portal is provided as the active portal.
    username cannot be null or empty.
    You must be signed on to get user content.
    You do not have permissions to access this resource or perform this operation.
    Request failed: status code {status code}.
    Remarks
    Executes the following portal rest query:
    {portalURL}sharing/rest/content/users/{userName}

    or {portalURL}sharing/rest/content/users/{userName}/{folderId} if a folderid was provided. Users executing this query must be signed on or an exception will be thrown.
    Example
    Workflow to open an ArcGIS Pro project
    // A portal project path looks like this:
    //@"https://<ServerName>.<Domain>.com/portal/sharing/rest/content/items/1a434faebbe7424d9982f57d00223baa";
    //A local project path looks like this:
    //@"C:\Users\<UserName>\Documents\ArcGIS\Projects\MyProject\MyProject.aprx";
    
    //Check if the project can be opened
    if (Project.CanOpen(projectPath, out docVer))
    {
      //Open the project
      await Project.OpenAsync(projectPath);
    }
    else //The project cannot be opened
    {
      //One possible reason: If the project is a portal project, the active portal must match the portal of the project
      //Check if this is a portal project
      bool isPortalProject = Uri.TryCreate(projectPath, UriKind.Absolute, out Uri uriResult)
           && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
    
      if (isPortalProject)
      {
        //Parse the project path to get the portal
        var uri = new Uri(projectPath);
        var portalUrlOfProjectToOpen = $"{uri.Scheme}://{uri.Host}/portal/";
    
        //Get the current active portal
        var activePortal = ArcGIS.Desktop.Core.ArcGISPortalManager.Current.GetActivePortal();
        //Compare to see if the active Portal is the same as the portal of the project
        bool isSamePortal = (activePortal != null && activePortal.PortalUri.ToString() == portalUrlOfProjectToOpen);
        if (!isSamePortal) //not the same. 
        {
          //Set new active portal to be the portal of the project
          //Find the portal to sign in with using its Uri...
          var projectPortal = ArcGISPortalManager.Current.GetPortal(new Uri(portalUrlOfProjectToOpen, UriKind.Absolute));
          await QueuedTask.Run(() => {
            if (!projectPortal.IsSignedOn())
            {
              //Calling "SignIn" will trigger the OAuth popup if your credentials are
              //not cached (eg from a previous sign in in the session)
              if (projectPortal.SignIn().success)
              {
                //Set this portal as my active portal
                ArcGISPortalManager.Current.SetActivePortal(projectPortal);
                return;
              }
            }
            //Set this portal as my active portal
            ArcGISPortalManager.Current.SetActivePortal(projectPortal);
          });
          //Now try opening the project again
          if (Project.CanOpen(projectPath, out docVer))
          {
            await Project.OpenAsync(projectPath);
          }
          else
          {
            System.Diagnostics.Debug.WriteLine("The project cannot be opened.");
          }
        }
        else //The portals are the same. So the problem could be something else - permissions, portal is down?
        {
          System.Diagnostics.Debug.WriteLine("The project cannot be opened.");
        }
      }
      else //Project is on disk and cannot be opened. 
      {
        System.Diagnostics.Debug.WriteLine("The project cannot be opened.");
      }
    }
    Retrieve a project item from a portal and open it
    var projectPortal = ArcGISPortalManager.Current.GetPortal(new Uri(@"https://<serverName>.<domain>.com/portal/", UriKind.Absolute));
    string owner = string.Empty;
    await QueuedTask.Run(() => {
      //Get the signed on user name
      owner = projectPortal.GetSignOnUsername();
    });
    //Get the user content from the portal
    var userContent = await projectPortal.GetUserContentAsync(owner);
    //Get the first portal project item
    var firstPortalProject = userContent.PortalItems.FirstOrDefault(pi => pi.PortalItemType == PortalItemType.ProProject);
    var portalProjectUri = firstPortalProject.ItemUri.ToString();
    //Check if project can be opened
    string docVer = string.Empty;
    if (Project.CanOpen(portalProjectUri, out docVer))
    {
      await Project.OpenAsync(portalProjectUri);
    }
    //Note: If Project.CanOpen returns false, the project cannot be opened. One reason could be 
    // the active portal is not the same as the portal of the project. Refer to the snippet: [Workflow to open an ArcGIS Pro project](ProSnippets-sharing#workflow-to-open-an-arcgis-pro-project)
    Portal: Get the user content for the active user from the active portal
    var portal = ArcGISPortalManager.Current.GetActivePortal();
    var owner = portal.GetSignOnUsername();
    var userContent = await portal.GetUserContentAsync(owner);
    //Get content for a specific folder (identified by its folder id)
    //var userContent = await portal.GetUserContentAsync(owner, folderId);
    
    //Get all the folders
    foreach (var pf in userContent.PortalFolders)
    {
      //Do something with the folders
    
    }
    
    //Get all the content items
    foreach (var pi in userContent.PortalItems)
    {
      //Do something with the portal items
    }
    
    Portal: Download any package items in the user content
    //user content previously from...
    //var userContent = await portal.GetUserContentAsync(owner);
    
    var packages = new List<PortalItemType>
    {
      PortalItemType.BasemapPackage,
      PortalItemType.GeoprocessingPackage,
      PortalItemType.LayerPackage,
      PortalItemType.LocatorPackage,
      PortalItemType.MapPackage,
      PortalItemType.ProjectPackage,
      PortalItemType.ScenePackage,
      PortalItemType.RulePackage,
      PortalItemType.VectorTilePackage
    };
    var folder = @"E:\Temp\PortalAPITest\";
    foreach (var di in userContent.PortalItems.Where(pi => packages.Contains(pi.PortalItemType)))
    {
      var path = System.IO.Path.Combine(folder, di.Name);
      await di.GetItemDataAsync(path);
    }
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.0 or higher.
    See Also