ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Desktop.Core Namespace / ArcGISPortal Class / GetSignOnUsername Method
Example

In This Topic
    GetSignOnUsername Method
    In This Topic
    Gets the username for the current connection This method should be called from within a QueuedTask or System.Threading.Task unless the caller is within ArcGIS.Desktop.Framework.Contracts.ConfigurationManager.OnApplicationInitializing in which case the main thread should be used.
    Syntax
    public string GetSignOnUsername()
    Public Function GetSignOnUsername() As String
    Exceptions
    ExceptionDescription
    Thrown when a method is called on the wrong Pro thread
    Example
    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;
      //Get the signed on user name
      owner = await QueuedTask.Run(() => projectPortal.GetSignOnUsername());
      //Get the user content from the portal
      PortalUserContent 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))
      {
        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 Current signed in User from the active portal
    {
      //Force login
      if (!portal.IsSignedOn())
      {
        portal.SignIn();
      }
      var user = portal.GetSignOnUsername();
    }
    Portal: Get the user content for the active user from the active portal
    {
      var activePortal = ArcGISPortalManager.Current.GetActivePortal();
      var owner = activePortal.GetSignOnUsername();
      var activeUserContent = await activePortal.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: Execute a portal search
    {
      var owner = portal.GetSignOnUsername();
      var portalInfo = await portal.GetPortalInfoAsync();
    
      //1. Get all web maps
      var query1 = PortalQueryParameters.CreateForItemsOfType(PortalItemType.WebMap);
    
      //2. Get all web maps and map services - include user, organization
      // and "usa" in the title
      var query2 = PortalQueryParameters.CreateForItemsOfTypes(new List<PortalItemType>() {
      PortalItemType.WebMap, PortalItemType.MapService}, owner, "", "title:usa");
      query2.OrganizationId = portalInfo.OrganizationId;
    
      //retrieve in batches of up to a 100 each time
      query2.Limit = 100;
    
      //Loop until done
      var portalItems = new List<PortalItem>();
      while (query2 != null)
      {
        //run the search
        PortalQueryResultSet<PortalItem> results = await portal.SearchForContentAsync(query2);
        portalItems.AddRange(results.Results);
        query2 = results.NextQueryParameters;
      }
      //process results
      foreach (var pi in portalItems)
      {
        //Do something with the portal items
      }
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also