Using Layers dataSource to print field names

3625
1
06-08-2015 03:18 PM
MikeMacRae
Occasional Contributor III

Hello,

I am trying to List the fields of each data source (which are all feature classes) of each layer in a map. The data source feature classes are SDE feature classes.

When I try this simple script:

mxd = arcpy.mapping.MapDocument(r"C:\Map_Documents\Test.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrList = arcpy.mapping.ListLayers(mxd, "", df)
for lyr in lyrList:
     if lyr.supports("dataSource"):
         for field in arcpy.ListFields(lyr.dataSource):
             print field.name

which throws the following error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "e:\sw_nt\arcgis\desktop10.2\arcpy\arcpy\__init__.py", line 1119, in ListFields
    return gp.listFields(dataset, wild_card, field_type)
  File "e:\sw_nt\arcgis\desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 344, in listFields
    self._gp.ListFields(*gp_fixargs(args, True)))
IOError: "C:\Documents and Settings\username\Application Data\ESRI\ArcCatalog\Connection to ThisSDE.sde\POPULATION_POINT" does not exist

indicating that the datasource does not exist. The problem is, that it does. My SDE feature classes are password protected, so i have a feeling that is why the script can't see each feature class and its associated fields. Can anyone suggest a work around to fix this?

0 Kudos
1 Reply
SunnyWisher
New Contributor III

Maybe sde connection manager?

http://resources.arcgis.com/en/help/main/10.1/index.html#//00s300000008000000

#Remove temporary connection file if it already exists
sdeFile = r"C:\Project\Output\TempSDEConnectionFile.sde"
if os.path.exists(sdeFile):
    os.remove(sdeFile)

#Create temporary connection file in memory
arcpy.CreateArcSDEConnectionFile_management(r"C:\Project\Output", "TempConnection", "myServerName", "5151", "myDatabase", "DATABASE_AUTH", "myUserName", "myPassword", "SAVE_USERNAME", "myUser.DEFAULT", "SAVE_VERSION")

#Export a map document to verify that secured layers are present
mxd = arcpy.mapping.MapDocument(r"C:\Project\SDEdata.mxd")
arcpy.mapping.ExportToPDF(mxd, r"C:\Project\output\SDEdata.pdf")

os.remove(sdeFile)
del mxd

40100 - List the data source of all layers in the table of contents of a map document via Python

OLD VB methods

Get Layer Data Source Name

16293 - Get the selected ArcMap layer programmatically with VBA

0 Kudos