ArcGIS Pro 3.5 API Reference Guide
ArcGIS.Desktop.Core Namespace / Project Class / OpenAsync Method
The full path or URL to the project or project package that will be opened
Example

In This Topic
    OpenAsync Method (Project)
    In This Topic
    Opens an existing project or project package
    Syntax
    public static Task<Project> OpenAsync( 
       string projectUri
    )
    Public Shared Function OpenAsync( _
       ByVal projectUri As String _
    ) As Task(Of Project)

    Parameters

    projectUri
    The full path or URL to the project or project package that will be opened

    Return Value

    A Task returning the project that was opened
    Exceptions
    Remarks

    ArcGIS Pro can open projects or project packages stored on a local or network computer, portal projects available from an ArcGIS Enterprise portal, and project packages available from an ArcGIS Enterprise portal or ArcGIS Online. Provide a valid file path or URL as appropriate. When a URL is provided, the portal associated with the URL must be the active portal, and you must sign in to the organization unless you are accessing a public project package.

    A local cache of the portal project is created in the download location specified by the GeneralOptions settings, then a project file (*.aprx) representing the portal project is opened by ArcGIS Pro.

    Alternatively, a project package stored in a portal is downloaded from the portal as determined by the DownloadOptions settings. All project packages are unpacked, then the project file (*.aprx) provided by the package is opened by ArcGIS Pro.

    If a project is currently open in ArcGIS Pro, it will be closed before the specified project is opened.

    Example
    Open an existing project
    //Opens an existing project or project package
    await Project.OpenAsync(@"C:\Data\MyProject1\MyProject1.aprx");
    Close a project
    //A project cannot be closed using the ArcGIS Pro API. 
    //A project is only closed when another project is opened, a new one is created, or the application is shutdown.
    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.");
      }
    }
    Get the portal from a portal project's path
     // 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 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 fullUri = $"{uri.Scheme}://{uri.Host}/portal/";
       System.Diagnostics.Debug.WriteLine($"The Url of the project is: {fullUri}");
       //Now get the ArcGISPortal object from the portal Uri
       var arcgisPortal = ArcGISPortalManager.Current.GetPortal(new Uri(fullUri, UriKind.Absolute));
       System.Diagnostics.Debug.WriteLine($"The portal of the project is: {arcgisPortal.PortalUri}");
       //Note: You can set the active portal to be the portal of the project. Refer to this snippet: [ArcGISPortalManager: Get a portal and Sign In, Set it Active](ProSnippets-sharing#arcgisportalmanager-get-a-portal-and-sign-in-set-it-active)
     }
    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)
    How to access Geoprocessing History
    string openProjectPath = @"D\DATA\IGPHistoryItemTestProject\IGPHistoryItemTestProject.aprx";
    await Project.OpenAsync(openProjectPath);
    MapProjectItem mapProjItem = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Equals("Map", StringComparison.CurrentCultureIgnoreCase));
    
    var map = await QueuedTask.Run(() => mapProjItem.GetMap());
    var ftrLayer = map.Layers[0] as FeatureLayer;
    string tool1 = "management.GetCount";
    var args1 = Geoprocessing.MakeValueArray(ftrLayer);
    var env = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
    GPExecuteToolFlags executeFlags = GPExecuteToolFlags.AddToHistory;
    var t = await Geoprocessing.ExecuteToolAsync(tool1, args1, env, null, null, executeFlags);
    
    IEnumerable<IGPHistoryItem> hisItems = Project.Current.GetProjectItemContainer(Geoprocessing.HistoryContainerKey) as IEnumerable<IGPHistoryItem>;
    
    String hitemID = "";
    String hitemToolPath = "";
    IGPResult hitemGPResult = null;
    DateTime hitemTimeStamp;
    
    foreach (var hitem in hisItems)
    {
      // common IGPHistoryItem and Item properties
      hitemID = (hitem as Item).ID;
      hitemToolPath = hitem.ToolPath;
      hitemGPResult = hitem.GPResult;
      hitemTimeStamp = hitem.TimeStamp;
    }
    
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.0 or higher.
    See Also