Only one project can be opened at a time in ArcGIS Pro. When another project is opened or a new project is created, this property is updated to reflect the newly opened project.
{
// not to be included in sample regions
var projectFolderConnection = Project.Current.GetItems<FolderConnectionProjectItem>().First();
// Get all the maps in a project
IEnumerable<MapProjectItem> projectMaps = Project.Current.GetItems<MapProjectItem>();
}
{
// Get all the folder connections in a project
IEnumerable<FolderConnectionProjectItem> projectFolders = Project.Current.GetItems<FolderConnectionProjectItem>();
// use projectFolders;
}
{
// Get all the server connections in a project
IEnumerable<ServerConnectionProjectItem> projectServers = Project.Current.GetItems<ServerConnectionProjectItem>();
// use projectServers;
}
{
// Get all the locator connections in a project
IEnumerable<LocatorsConnectionProjectItem> projectLocators = Project.Current.GetItems<LocatorsConnectionProjectItem>();
// use projectLocators;
}
{
// Get all the items that can be accessed from a folder connection. The items immediately
// contained by a folder, that is, the folder's children, are returned including folders
// and individual items that can be used in ArcGIS Pro. This method does not return all
// items contained by any sub-folder that can be accessed from the folder connection.
FolderConnectionProjectItem folderConnection = Project.Current.GetItems<FolderConnectionProjectItem>()
.FirstOrDefault(folder => folder.Name.Equals("Data"));
IEnumerable<Item> folderContents = await QueuedTask.Run(() => folderConnection.GetItems());
}
{
// Add a folder connection to a project
Item folderToAdd = ItemFactory.Instance.Create(@"C:\Data\Oregon\Counties\Streets");
var project = Project.Current;
bool wasAdded = await QueuedTask.Run(() => project.AddItem(folderToAdd as IProjectItem));
// use wasAdded;
}
{
// Add a file geodatabase or a SQLite or enterprise database connection to a project
Item gdbToAdd = folderToAdd.GetItems().FirstOrDefault(folderItem => folderItem.Name.Equals("CountyData.gdb"));
var project = Project.Current;
var addedGeodatabase = await QueuedTask.Run(() => project.AddItem(gdbToAdd as IProjectItem));
// use addedGeodatabase;
}
{
// Remove a folder connection from a project; the folder stored on the local disk
// or the network is not deleted
var project = Project.Current;
await QueuedTask.Run(() =>
{
bool result = false;
FolderConnectionProjectItem folderToRemove = project.GetItems<FolderConnectionProjectItem>()
.FirstOrDefault(folder => folder.Name.Equals("Data"));
if (folderToRemove != null)
{
result = project.RemoveItem(folderToRemove as IProjectItem);
}
// use result
});
}
{
// Remove a map from a project; the map is deleted
IProjectItem mapToRemove = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(map => map.Name.Equals("OldStreetRoutes"));
var project = Project.Current;
var removedMapProjectItemResult = await QueuedTask.Run(() => project.RemoveItem(mapToRemove));
// Use removedMapProjectItemResult
}
{
// Import a mxd
Item mxdToImport = ItemFactory.Instance.Create(@"C:\Projects\RegionalSurvey\LatestResults.mxd");
var project = Project.Current;
var addedMxd = await QueuedTask.Run(() => project.AddItem(mxdToImport as IProjectItem));
// Add map package
Item mapPackageToAdd = ItemFactory.Instance.Create(@"c:\Data\Map.mpkx");
var addedMapPackage = await QueuedTask.Run(() => project.AddItem(mapPackageToAdd as IProjectItem));
// Add an exported Pro map
Item proMapToAdd = ItemFactory.Instance.Create(@"C:\ExportedMaps\Election\Districts.mapx");
var addedMapProjectItem = await QueuedTask.Run(() => project.AddItem(proMapToAdd as IProjectItem));
}
{
FolderConnectionProjectItem projectfolderConnection = Project.Current.GetItems<FolderConnectionProjectItem>().First();
var folderConnectionContent = projectfolderConnection.GetItems();
var folder = folderConnectionContent.FirstOrDefault(folderItem => folderItem.Name.Equals("Tourist Sites"));
var folderContents = folder.GetItems();
//use folderContents;
}
{
StyleProjectItem styleItem = Project.Current.GetItems<StyleProjectItem>().
FirstOrDefault(style => style.Name == "ArcGIS 3D");
if (FavoritesManager.Current.CanAddAsFavorite(styleItem))
{
// add to favorites with IsAddedToAllNewProjects set to false
FavoritesManager.Current.AddFavorite(styleItem);
}
}
{
projectPath = Project.Current.Url;
// 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";
bool isPortalProject = Uri.TryCreate(projectPath, UriKind.Absolute, out Uri uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (isPortalProject)
{
System.Diagnostics.Debug.WriteLine("This is a portal project");
}
else
{
System.Diagnostics.Debug.WriteLine("This is not a portal project");
}
// Use isPortalProject;
}
{
var isPortalProject = Project.Current.IsPortalProject;
// Use isPortalProject;
}
{
projectPath = Project.Current.Url;
// 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)
}
}
{
//Gets the current project
var project = Project.Current;
// Use project
}
{
//Gets the location of the current project; that is, the path to the current project file (*.aprx)
projectPath = Project.Current.URI;
// Use projectPath
}
{
var projGDBPath = Project.Current.DefaultGeodatabasePath;
// Use projGDBPath;
}
{
//Create a new GDB item and add it to the project
if (ItemFactory.Instance.Create(newGDDItemPath) is not IProjectItem newGDBItem)
{
// could not create the item
return;
}
var success = Project.Current.AddItem(newGDBItem);
//make the newly added GDB item the default
if (success)
{
Project.Current.SetDefaultGeoDatabasePath(newGDDItemPath);
}
//Now remove the old item
if (Project.Current.GetItems<Item>().FirstOrDefault(i => i.Path == oldGDBItemPath) is not IProjectItem oldGDBItem)
{
// could not find the item
return;
}
var removeSuccess = Project.Current.RemoveItem(oldGDBItem);
}
{
//Saves the project
var success = await Project.Current.SaveAsync();
}
{
//The project's dirty state indicates changes made to the project have not yet been saved.
bool isProjectDirty = Project.Current.IsDirty;
// Use isProjectDirty;
}
{
//Saves a copy of the current project file (*.aprx) to the specified location with the specified file name,
//then opens the new project file
var success = await Project.Current.SaveAsAsync(newProjectPath);
}
{
//Adding a folder connection
//Create the folder connection project item
var item = ItemFactory.Instance.Create(folderPath) as IProjectItem;
var folder = await QueuedTask.Run(() =>
Project.Current.AddItem(item) ? item as FolderConnectionProjectItem : null);
}
{
//Adding a Geodatabase:
await QueuedTask.Run(() =>
{
//Create the File GDB project item
var item = ItemFactory.Instance.Create(gdbPath) as IProjectItem;
var newlyAddedGDB = Project.Current.AddItem(item) ? item as GDBProjectItem : null;
});
}
{
IEnumerable<Item> allProjectItems = Project.Current.GetItems<Item>();
foreach (var pi in allProjectItems)
{
//Do Something
}
}
{
var project = Project.Current;
IEnumerable<MapProjectItem> newMapItemsContainer = project.GetItems<MapProjectItem>();
await QueuedTask.Run(() =>
{
foreach (var mp in newMapItemsContainer)
{
//Do Something with the map. For Example:
Map myMap = mp.GetMap();
}
});
}
{
MapProjectItem mapProjItem = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Equals("EuropeMap"));
// Use mapProjItem;
}
{
IEnumerable<StyleProjectItem> newStyleItemsContainer = null;
newStyleItemsContainer = Project.Current.GetItems<StyleProjectItem>();
foreach (var styleItem in newStyleItemsContainer)
{
//Do Something with the style.
}
}
{
var container = Project.Current.GetItems<StyleProjectItem>();
// example: projectStyleName = "ArcGIS 3D";
StyleProjectItem testStyle = container.FirstOrDefault(style => style.Name == projectStyleName);
StyleItem cone = null;
if (testStyle != null)
{
// example: symbolName = "Cone_Volume_3";
cone = testStyle.LookupItem(StyleItemType.PointSymbol, symbolName);
}
// Use (testStyle, cone);
}
{
await QueuedTask.Run(() =>
{
var containerStyle = Project.Current.GetProjectItemContainer("Style");
var fav_style_item = containerStyle.GetItems().OfType<StyleProjectItem>().First(item => item.TypeID == "personal_style");
});
}
{
IEnumerable<GDBProjectItem> newGDBItemsContainer = null;
newGDBItemsContainer = Project.Current.GetItems<GDBProjectItem>();
foreach (var GDBItem in newGDBItemsContainer)
{
//Do Something with the GDB.
}
}
{
GDBProjectItem gdbProjItem = Project.Current.GetItems<GDBProjectItem>().FirstOrDefault(item => item.Name.Equals("myGDB"));
// Use gdbProjItem;
}
{
IEnumerable<ServerConnectionProjectItem> newServerConnections = null;
var project = Project.Current;
newServerConnections = project.GetItems<ServerConnectionProjectItem>();
foreach (var serverItem in newServerConnections)
{
//Do Something with the server connection.
}
}
{
ServerConnectionProjectItem serverProjItem = Project.Current.GetItems<ServerConnectionProjectItem>().FirstOrDefault(item => item.Name.Equals("myServer"));
// Use serverProjItem;
}
{
//Gets all the folder connections in the current project
var projectFolders = Project.Current.GetItems<FolderConnectionProjectItem>();
foreach (var FolderItem in projectFolders)
{
//Do Something with the Folder connection.
}
}
{
FolderConnectionProjectItem folderProjItem = Project.Current.GetItems<FolderConnectionProjectItem>().FirstOrDefault(item => item.Name.Equals("myDataFolder"));
// Use folderProjItem;
}
{
// Remove a folder connection from a project; the folder stored on the local disk or the network is not deleted
FolderConnectionProjectItem folderToRemove = Project.Current.GetItems<FolderConnectionProjectItem>().FirstOrDefault(myfolder => myfolder.Name.Equals("PlantSpecies"));
if (folderToRemove != null)
{
Project.Current.RemoveItem(folderToRemove as IProjectItem);
}
}
{
LayoutProjectItem layoutProjItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("myLayout"));
// Use layoutProjItem;
}
{
//Gets all the layouts in the current project
IEnumerable<LayoutProjectItem> projectLayouts = Project.Current.GetItems<LayoutProjectItem>();
foreach (var layoutItem in projectLayouts)
{
//Do Something with the layout
}
}
{
//Gets a specific GeoprocessingProjectItem in the current project
// example: "myToolbox" is the name of the GeoprocessingProjectItem
GeoprocessingProjectItem gpProjItem = Project.Current.GetItems<GeoprocessingProjectItem>().FirstOrDefault(item => item.Name.Equals("myToolbox"));
// Use gpProjItem;
}
{
//Gets all the GeoprocessingProjectItem in the current project
var GPItems = Project.Current.GetItems<GeoprocessingProjectItem>();
foreach (var tbx in GPItems)
{
//Do Something with the toolbox
}
}
{
List<Item> _mxd = [];
//Gets all the folder connections in the current project
var allFoldersItem = Project.Current.GetItems<FolderConnectionProjectItem>();
if (allFoldersItem != null)
{
//iterate through all the FolderConnectionProjectItems found
foreach (var folderItem in allFoldersItem)
{
//Search for mxd files in that folder connection and add it to the List<T>
//Note:ArcGIS Pro automatically creates and dynamically updates a searchable index as you build and work with projects.
//Items are indexed when they are added to a project.
//The first time a folder or database is indexed, indexing may take a while if it contains a large number of items.
//While the index is being created, searches will not return any results.
_mxd.AddRange(folderItem.GetItems());
}
}
}
{
var contentItem = Project.Current.GetItems<FolderConnectionProjectItem>().First();
//Check if the MCT is required for Refresh()
if (contentItem.IsMainThreadRequired)
{
await QueuedTask.Run(() => contentItem.Refresh());
}
else
{
//if item.IsMainThreadRequired returns false, any
//thread can be used to invoke Refresh(), though
//BackgroundTask is preferred.
contentItem.Refresh();
//Or, via BackgroundTask
ArcGIS.Core.Threading.Tasks.BackgroundTask.Run(() =>
contentItem.Refresh(), ArcGIS.Core.Threading.Tasks.BackgroundProgressor.None);
}
}
{
//Use Project.Current.ProjectItemContainers
var folderContainer = Project.Current.ProjectItemContainers.First(c => c.Path == "FolderConnection");
var gdbContainer = Project.Current.ProjectItemContainers.First(c => c.Path == "GDB");
var mapContainer = Project.Current.ProjectItemContainers.First(c => c.Path == "Map");
var layoutContainer = Project.Current.ProjectItemContainers.First(c => c.Path == "Layout");
var toolboxContainer = Project.Current.ProjectItemContainers.First(c => c.Path == "GP");
//etc.
//or...use Project.Current.GetProjectItemContainer
folderContainer = Project.Current.GetProjectItemContainer("FolderConnection");
gdbContainer = Project.Current.GetProjectItemContainer("GDB");
mapContainer = Project.Current.GetProjectItemContainer("Map");
layoutContainer = Project.Current.GetProjectItemContainer("Layout");
toolboxContainer = Project.Current.GetProjectItemContainer("GP");
//etc.
}
{
//GetItems searches project content
var map = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(m => m.Name == "Map1");
var layout = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(m => m.Name == "Layout1");
var folders = Project.Current.GetItems<FolderConnectionProjectItem>();
var style = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS 3D");
//Find item uses a catalog path. The path can be to a file or dataset
var fcPath = @"C:\Pro\CommunitySampleData\Interacting with Maps\Interacting with Maps.gdb\Crimes";
var pdfPath = @"C:\Temp\Layout1.pdf";
var imgPath = @"C:\Temp\AddinDesktop16.png";
var fc = Project.Current.FindItem(fcPath);
var pdf = Project.Current.FindItem(pdfPath);
var img = Project.Current.FindItem(imgPath);
}
{
//Get the catalog pane
IProjectWindow projectWindow = Project.GetCatalogPane();
//or get the active catalog view...
//ArcGIS.Desktop.Core.IProjectWindow projectWindow = Project.GetActiveCatalogWindow();
//eg Find a toolbox in the project
string gpName = "Interacting with Maps.tbx";
var toolbox = Project.Current.GetItems<GeoprocessingProjectItem>().FirstOrDefault(tbx => tbx.Name == gpName);
//Select it under Toolboxes
await projectWindow.SelectItemAsync(toolbox, true, true, null);//null selects it in the first container
//Note: Project.Current.GetProjectItemContainer("GP") would get toolbox container...
//assume toolbox is also under Folders container. Select it under Folders instead of Toolboxes
var foldersContainer = Project.Current.ProjectItemContainers.First(c => c.Path == "FolderConnection");
//We must specify the container because Folders comes second (after Toolboxes)
await projectWindow.SelectItemAsync(toolbox, true, true, foldersContainer);
//Find a map and select it
var mapItem = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(m => m.Name == "Map");
//Map only occurs under "Maps" so the container need not be specified
projectWindow.SelectItemAsync(mapItem, true, false, null);
}
Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)