Select to view content in your preferred language

ArcPy Show Stopper:  Automated scripts require user input via dialog boxes?

3647
10
06-21-2010 05:59 AM
KeithFraley
Emerging Contributor
I am in the process of writing a crawler that does certain things. One of its primary tasks is to look inside mxds and layer files and find out what layers the documents has, and what datasources they point to.  The ArcPy describe method is pretty much designed to just this very thing, and for the most part it does it quite well. 

However, using this bit of code (below) to find an SDE or ArcGIS Server Reference works well, except if ArcPy cant find the SDE Instance or the AGS Service.  At that point, ArcPy will prompt the user to input the parameters to validate the layer.  This does not go over well when there is no one around to babysit the crawler. 

Is there a way to make ArcPy not automatically prompt the user for input or I am not approaching this the right way?


if lyr.supports("SERVICEPROPERTIES"):
   if lyr.serviceProperties["ServiceType"] == "SDE":
      SDE_HOST = lyr.serviceProperties.get('Server', 'N/A').upper()
      SDE_PORT = lyr.serviceProperties.get('Service', 'N/A').upper()
   if lyr.serviceProperties["ServiceType"] == "MapServer":
      SOURCE_PATH = lyr.serviceProperties.get('URL', 'N/A')
0 Kudos
10 Replies
JeffBarrette
Esri Regular Contributor
Hello Keith,

Secured layers need to have credential information passed in.  When you connect to MXDs that have secured layers you must also provide the connection information.  When you do this via the User Interface, a dialog will prompt you to enter the information.  When you do this via arcpy.mapping, at final, the dialog is suppressed but you still need to enter the connection info.  This is done using the CreateArcSDEConnectionFile function.  See the second sample code snippet available for the Layer Class in arcpy.mapping help.

In you senario below, the first "IF" statement fails because if you attempt to open a map document with secured layers and you DON'T provide connection info, the layer is essentially broken and server specific information can't be obtained.  The layer can't support "serviceproperties" because the layer is broken and we can't get the information.  Execute CreateArcSDEConnectionFile just before those lines of code and the connection info will be persisted in memory and the MXD will open with the connections established.


import arcpy

arcpy.CreateArcSDEConnectionFile_management(r"C:\Project\Output",  "TempConnection", "xxx", "5152", "", "DATABASE_AUTH", "xxx", "xxx", "SAVE_USERNAME", sde.DEFAULT", "SAVE_VERSION")
  
mxd = arcpy.mapping.MapDocument(r"C:\temp\SDEandAGSdata.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("SERVICEPROPERTIES"):
        if lyr.serviceProperties["ServiceType"] == "SDE":
            print "SDE"
        if lyr.serviceProperties["ServiceType"] == "MapServer":
            print "SERVER"

I hope this helps,
Jeff
0 Kudos
Kathleen_Crombez
Frequent Contributor

This does not seem to work...

I am using CreateDatabaseConnection_management instead of CreateArcSDEConnectionFile_management because I want to make a direct connection to SQL Server instead of an application connection.

But it is NOT suppressing the dialog box.

The script pauses and will not continue until I click cancel or close the dialog box.

Here is my code....

sde = arcpy.CreateDatabaseConnection_management("foldername", "filename", "SQL_SERVER", "servername", "DATABASE_AUTH", "username", "password", "SAVE_USERNAME", "database", "#", "TRANSACTIONAL", "sde.DEFAULT")

mxd = arcpy.mapping.MapDocument("path to mxd")

Seems like arcpy.mapping.MapDocument should be rewritten to accept login credential parameters.

0 Kudos
RobDunfey
Regular Contributor
Hi Jeff,

Like Keith we also have a problem with some python automation, we are prompted for a username and password for 'secured' arcims feature class services.  This is a nightmare as we need to run overnight.  Reading the help for ListBrokenDataSources I spotted:

�??The arcpy.mapping scritping environment will, by default, supress these dialogs during execution�?�

Any ideas how we can get this to work?

Rob
0 Kudos
MeetaGoel
New Contributor
I have similar arcpy trouble with automation. I have a batch script that calls 2 python scripts for geocoding using the premium subscription for world geocoding. Although I have saved userid and password in arccatalog, the python script randomly shows GIS server connectino dialog (already filled in userid, password). ESRI support said it was a logon timeout issue (12 hours) but I get this problem within same hour also. Its just random. No pattern.
Any help will be appreciated.

Thanks
0 Kudos
MichaelBlom
Deactivated User
I too am trying to run automated scripts that use sde (direct) connections, but are failing when a dialog prompts me to specify if the connection "refers to a transactional version" or "refers to a historical version".  I don't need to enter any details, just press "ok" and the script continues on. 

Unfortunately this dialog prompt is standing in the way of the fundamental concept behind "automation".

Maybe I'll start a new thread on this to get more help.

Mike
0 Kudos
Kathleen_Crombez
Frequent Contributor

I am having the same problem. Did you ever find a work around?

0 Kudos
GregSpiridonov
Occasional Contributor

You may be getting such issues because you are not logged in to a secured mapservice, not the SDE.
A mapservice connection is not connected to an SDE connection and would require it's own credentials. When adding such a service in ArcMap, it will use the username and password of the GIS Server connection, so you don't have to put it in, but it is there. Check  your mapservice layers in Properties>Source and if you see a username under "User" then it is using a username/password to access that service.

As for how to do that in python, I'm not sure. I'm assuming it would be in the 'N/A' portion of your code below, much like in the SDE connections.

if lyr.serviceProperties["ServiceType"] == "MapServer":

      SOURCE_PATH = lyr.serviceProperties.get('URL', 'N/A')

Hope this helps!

0 Kudos
GregSpiridonov
Occasional Contributor

(of course if all your map services are public, then unfortunately that will not help)

0 Kudos
Kathleen_Crombez
Frequent Contributor

The dialog box is prompting for SDE connection credentials.

So far the old map documents I have been testing are so old (2005) that ArcGIS Server did not even exist yet.

I manually opened one of the trouble documents and the issue is a secured layer that does not have the login credentials saved.

0 Kudos