Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a loaded map document (mxd) how do I find the geodatabase objects related to the contained layers? I want to copy them with the map document so the map can be moved onto each user's local machine from a network share. Is there an API to do this? Can someone point to source code that does this?

To clarify (after all the great feedback): How is a "Map Package" created programmatically using the ArcEngine API? Is this even possible?

share|improve this question
up vote 1 down vote accepted

There are many ways to accomplish this outside of the api.
Which will determine what approach you use "in" the API.
Start here and look for replication, layer package, map package, export.
For a few of the ways it could be accomplished.
Then perhaps it will be easier to help you with the API direction.
ESRI Web Help

I did find this on the forum...
but it is from 2010
esri forum
Another hint might be that if you look at a layer package and rename it from lpk to zip - it does exactly that. unzips.

import os
import sys
import arcpy
import arcpy.mapping as mapping

# get input parameters 
outDir = arcpy.GetParameterAsText(0)
packageMap = arcpy.GetParameter(1)
checkBrokenLayers = arcpy.GetParameter(2)

# get the map document in which this code is running ("Current" keyword)
mxd = mapping.MapDocument('Current')

# build a pathname to the output report (text file)
reportPath = outDir + '\\' + mxd.title + '.txt'
# open the file (will be created if it doesn't exist)
reportFile = open(reportPath, 'w')
arcpy.AddMessage('Writing report to ' + reportPath)

# start writing report text to a string variable
reportText = 'Title: ' + mxd.title + '\n'
reportText += 'Author: ' + + '\n'
reportText += 'Description: ' + mxd.description + '\n'
reportText += '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' + '\n'

# if the user chose to do so, create map package
if packageMap:
    packagePath = outDir + '\\' + mxd.title.replace('.', '_') + '.mpk'
    if (os.path.exists(packagePath)):
    arcpy.AddMessage('Map package already exists (' + packagePath + ')')
    arcpy.AddMessage('Creating map package (' + packagePath + ')' )
    arcpy.PackageMap_management(mxd.filePath, packagePath)

# loop thru all data frames in the map
dataFrames = mapping.ListDataFrames(mxd, '')
for frame in dataFrames:
    # report data frame name and spatial reference
    reportText += '\nData Frame: ' + + '\n'
    reportText += 'Spatial Reference: ' + + '\n'
    # get all layers in this data frame
    layers = mapping.ListLayers(mxd, '', frame)
    i = 0 # layer index position
    # loop thru all layers in the data frame
    for lyr in layers:
        # report index position and name
    reportText += '\tLayer ' + str(i) + ': ' + + '\n'
    i += 1 # same as i = i + 1

# if the user has requested it, check for layers with a missing data source
if checkBrokenLayers:
    arcpy.AddMessage('Checking for missing data sources')
    brokenList = mapping.ListBrokenDataSources(mxd)
    # report the count of broken layers
    reportText += '\nFound ' + str(len(brokenList)) + ' layers with missing data.'
    # loop thru all broken layers in the list
    for broken in brokenList:
        # report broken layer name
    reportText += '\t- ' + + '\n'

# write the text stored in the reportText variable to the output file
reportFile.close() # close the file
del mxd # delete the mxd object
share|improve this answer
I'm stuck with API. I'm not in the Desktop - I'm in a standalone viewer. If I can't code it then it will not happen. :-) – Chaz Feb 14 '12 at 23:11
Thanks for the info tho - at least I know I want to create a "map package" with no help from ESRI. – Chaz Feb 14 '12 at 23:19
@Chaz I'd also like to know how to create a map package programmatically, maybe include that in your question. – Kirk Kuykendall Feb 15 '12 at 0:32
I'm on this quest and the results will be shared. Thanks to everyone who is helping me get pointed in the correct direction. – Chaz Feb 17 '12 at 15:41

This is what ESRI told me. Of course this uses the ArcPy toolbox. But ArcPy is installed with every version of ArcGIS software (including the runtime engine.) Note that you have to pass the full pathname for the map file and the target package file. In addition, the help warns you that you must set the "Description" for the map file or this WILL NOT WORK. Of course there is no such thing as a "Description" property for a map file. Your file "SAVE" must include: documentInfo.Comments = "Some Comment required."; (Cast MapDocument to IDocumentInfo (or IDocumentInfo2) and set the Comments property.)

using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataManagementTools;

Geoprocessor geoprocessor = new Geoprocessor();
PackageMap packageMap = new PackageMap();
packageMap.in_map = mapFile;
packageMap.output_file = packageFile;
geoprocessor.Execute( packageMap, null );

For now I'll live with this - I sure hope they decide to provide lower level code in 10.1.1 (even tho 10.1 is trying to abstract more.)

share|improve this answer

Another option is to use an arcpy script to list the layers in a particular mxd without opening it manually. The arcpy.mapping sample script tools has a script called Multi MXD Summary (Report), which does this. You could further customize the script to automatically move mxd's that contain geodatabase files (moving the mxd and the geodatabase).

share|improve this answer
As I understand it I can only use scripts if user has Desktop installed. Is that correct? – Chaz Feb 15 '12 at 17:09
@Chaz, I think you need at least one desktop that has ArcGIS installed. If so, you can then reference arcpy against that desktop, see… – artwork21 Feb 15 '12 at 17:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.