Working with packages (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with packages (ArcObjects .NET 10.6 SDK)

Working with packages


Summary
Starting at ArcGIS 10, sp 1 packages and Web maps can be accessed programmatically. This topic shows you how you can programmatically consume layer packages, map packages, and Web maps.

In this topic

Opening layer packages

The LayerFile class has been enhanced to include the ability to open layer packages from both local data sources and from ArcGIS.com. The following code example shows how to open a local layer package and add it to a MapControl:
[VB.NET]
Dim layerFile As ILayerFile=New LayerFileClass
layerFile.Open("c:\Data\LayerPackages\USCities.lpk")
Dim layer As ILayer=layerFile.Layer
axMapControl1.AddLayer(layer)
[C#]
ILayerFile layerFile=new LayerFileClass();
layerFile.Open(@"c:\Data\LayerPackages\USCities.lpk");
ILayer layer=layerFile.Layer;
axMapControl1.AddLayer(layer);
ArcGIS.com layer packages can be access by specifying the appropriate uniform resource locator (URL) to the package instead of the path. See the following section to see how to properly format a URL to access ArcGIS.com packages.
Layer packages can contain multiple layers. When opening layer packages programmatically, only the first layer will be added.   

Opening map packages

The MapDocument class has also been enhanced to open map packages from both local data sources and ArcGIS.com. The following code example opens a map package and assigns the first data frame to the map in the MapControl.
[VB.NET]
Dim mapDocument As IMapDocument=New MapDocumentClass()
mapDocument.Open("c:\Data\LayerPackages\MyMapPackage.mpk", "")
axMapControl1.Map=mapDocument.get_Map(0)
[C#]
IMapDocument mapDocument=new MapDocumentClass();
mapDocument.Open(@"c:\Data\LayerPackages\MyMapPackage.mpk", "");
axMapControl1.Map=mapDocument.get_Map(0);
ArcGIS.com map packages can be accessed by specifying the appropriate URL to the package instead of the path. See the following section to see how to properly format a URL to access the previously mentioned ArcGIS.com packages.
The MapDocument class can also be used to load layer packages.

Accessing packages from ArcGIS Online

ArcGIS Online content can be accessed by replacing the path to the file with the appropriate URL to the content. The Open method for ILayerFile will open ArcGIS.com layer packages. The IMapDocument.Open method will open ArcGIS Online layer packages, map packages and Web maps. To formulate the correct URL, determine what the correct URL is for the content. For example, review the Bing Maps Hybrid layer package on ArcGIS.com.
The unique identifier is 71d6d656cb2a4ded8fce35982ebdff25. Plug the unique identifier for the following URL:
Use this URL instead of the file name to open the layer package. The following code example opens the Bing Maps Hybrid Layer package from ArcGIS.com:
[VB.NET]
Dim layerFile As ILayerFile=New LayerFileClass
layerFile.Open("http://www.arcgis.com/sharing/content/items/71d6d656cb2a4ded8fce35982ebdff25/item.pkinfo")
Dim layer As ILayer=layerFile.Layer
axMapControl1.AddLayer(layer)
[C#]
ILayerFile layerFile=new LayerFileClass();
layerFile.Open(@
    "http://www.arcgis.com/sharing/content/items/71d6d656cb2a4ded8fce35982ebdff25/item.pkinfo");
ILayer layer=layerFile.Layer;
axMapControl1.AddLayer(layer);
The same logic can be used for opening map packages with the MapDocument class.

Opening Web maps

The Open method under IMapDocument can also be used to open Web maps. For example, see the following link for the World Street map Web map:
By placing the unique identifier into the same URL format you used for the Bing Maps layer package, you can open a Web Map programmatically. See the following code example:
[VB.NET]
Dim mapDocument As IMapDocument=New MapDocumentClass()
mapDocument.Open("http://www.arcgis.com/sharing/content/items/3b93337983e9436f8db950e38a8629af/item.pkinfo", "")
axMapControl1.Map=mapDocument.get_Map(0)
[C#]
IMapDocument mapDocument=new MapDocumentClass();
mapDocument.Open(@
    "http://www.arcgis.com/sharing/content/items/3b93337983e9436f8db950e38a8629af/item.pkinfo", "");
axMapControl1.Map=mapDocument.get_Map(0);

Accessing unpacked data

Once a package has been opened, the data is unpacked and stored on disk. This unpacking process is handled by the ArcGISFileHandler, which is found in the \Program Files\Common Files\ArcGIS\bin folder. By default, the data is unpacked into the %userprofile%\My Documents\ArcGIS\Packages folder.  
Web maps are saved as .mxd files in the opened %userprofile%\My Documents\ArcGIS\Packages Web Maps folder. Individual clients can change the default location of where packages are opened through the ArcGISFileHandler. These changes are stored in the registry under HKCU\Software\ESRI\ArcGIS File Handler\Settings. Developers can check the "PackageLocationOption" registry key to determine if the path is the default (value 0) or a different path (value 1). From that, developers can use the "PackageLocation" value to determine what the new path is.     

Leveraging ArcGIS.com

ArcGIS Online content can be used as a data deployment mechanism for a custom application. A package on ArcGIS Online will only download if changes have been made to the data. Developers can always point to their package on ArcGIS Online. If the data is current it will use the unpacked data on disk. If the data is updated, it downloads the new package. 
Developers can create unique groups and logins for their online content to restrict their data to specific users.  When trying to access private ArcGIS Online data the user will be prompted with a sign on dialog where must provide their credentials.       
Signing in to ArcGIS Online will start the ArcGISConnection executable which will show up as a globe icon on your taskbar.  This executable is responsible for managing your ArcGIS Online tokens, and will exit on its own when it is no longer needed.  There is no need of the developer or end user to do anything. 


See Also:

Working with packages in ArcGIS Engine




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine