Create an ArcGIS Server Connection file Programmatically

3896
4
02-18-2011 12:58 PM
AllanBenvin
New Contributor III
I'm trying to write a batch Python job that will connect to AGServer to geocode some data. I don't want to store a connection file with this I want to connect dynamically. My Python script calls some C# code that I am writing that accepts the server name and creates a connection. This is all good but apparently I am not smart enough to create a connection file once I have made the connection. I'm sure this must be possible but I'm not finding it in the help.
My code to make the connection is below.

string pServer = "myServer";
IAGSServerConnectionFactory2 AGSConnectionFactory = (IAGSServerConnectionFactory2)new   
            AGSServerConnectionFactory();
IPropertySet2 connectionProps = (IPropertySet2)new PropertySet();
connectionProps.SetProperty("machine", pServer);
IAGSServerConnection3 myAGSConnection = (IAGSServerConnection3)AGSConnectionFactory.Open
          (connectionProps, 0);

At this point I have the connection so I know that works but how do I make a connection file that can then be accessed by Python script?

Any help would be appreciated.

-Al
0 Kudos
4 Replies
MonikaNientiedt
Esri Contributor
Al,

there is a tool in Data Management Tools -> Workspace that lets you create a sde connection file.
You could create the file directly from within your Python script.
The following code is from the Desktop Help:

# CreateArcSDEConnection.py
# Description: Simple example showing use of CreateArcSDEConnectionFile tool
# Author: ESRI

# Import system modules
import arcpy

# Set variables
folderName = r"c:\connectionFiles"
fileName = "Connection to gpserver.sde"
serverName = "gpserver"
serviceName = "5151"
databaseName = ""
authType = "DATABASE_AUTH"
username = "toolbox"
password = "toolbox"
saveUserInfo = "SAVE_USERNAME"
versionName = "SDE.DEFAULT"
saveVersionInfo = "SAVE_VERSION"

#Process: Use the CreateArcSDEConnectionFile function
arcpy.CreateArcSDEConnectionFile_management (folderName, fileName, serverName, serviceName, databaseName, authType, username, password, saveUserInfo, versionName, saveVersionInfo)


Monika
0 Kudos
AllanBenvin
New Contributor III
This will create a connection for ArcSDE but I want to create a connection file for ArcGIS Server to access a geocoding service. To do this manually it in Catalog it would be GIS Servers-Add ArcGIS Servers-Use GIS Services--http://myserver/arcgis/services
0 Kudos
AllanBenvin
New Contributor III
Okay, I got it figured out. I had to go use IAGSServerConnectionName2 to make the connection and then go to the Catalog library to get IGxAGSConnection to save as a file.

Code snippet is below:

string myfullpath = @"c:\temp\myagsConn.ags";
IAGSServerConnectionName2  myAGSConnName2 =(IAGSServerConnectionName2) new AGSServerConnectionName();
IPropertySet connectionProps = new PropertySet();

connectionProps.SetProperty("URL",targetURL);
connectionProps.SetProperty("HTTPTIMEOUT",targetTimeout);
connectionProps.SetProperty("ANONYMOUS",true);
myAGSConnName2.ConnectionProperties = connectionProps;
       
IGxAGSConnection myAGConn = (IGxAGSConnection) new  GxAGSConnection();
myAGConn.AGSServerConnectionName =  (IAGSServerConnectionName2)myAGSConnName2;
myAGConn.Connect();

myAGConn.SaveToFile(myfullpath);
Console.WriteLine("Created file: "   + myfullpath );
0 Kudos
MonikaNientiedt
Esri Contributor
Hi Al,
Great! I clearly misread your original post.
0 Kudos