Skip to content

Instantly share code, notes, and snippets.

@odoe
Created April 26, 2012 20:07
Show Gist options
  • Save odoe/2502686 to your computer and use it in GitHub Desktop.
Save odoe/2502686 to your computer and use it in GitHub Desktop.
Arcpy script to add data to mxd and publish to ArcGIS server without ArcMap
'''
Created on Feb 24, 2011
The purpose of this script is to create the SDE connection file needed to connect to your SDE
@author: rrubalcava
'''
import os, arcpy
class CreateSDEConnection:
def __init__(self, folder):
self.folder = folder
self.connection = folder + "connection.sde"
def createConnection(self):
print "Checking if SDE Connection file already exists"
if os.path.exists(self.connection):
print "SDE Connection file already exists, delete it"
os.remove(self.connection)
print "continue with creating sde connection file"
fileName = "connection.sde"
server = "SERVER_NAME"
# update this if not using SQL Server
instance = "sde:sqlserver:SERVER_NAME"
db = "DATABASE_INSTANCE"
authType = "DATABASE_AUTH"
user = "USER_NAME"
pw = "PASSWORD"
saveUserInfo = "SAVE_USERNAME"
# I just use default here, but you can change it
version = "SDE.Default"
saveVersionInfo = "SAVE_VERSION"
try:
arcpy.CreateArcSDEConnectionFile_management(self.folder, fileName, server, instance, db, authType, user, pw, saveUserInfo, version, saveVersionInfo)
print "SDE Connection file created successfully"
#self.log.trace('SDE Connection created successfully')
return True
except arcpy.ExecuteError, ex:
print "An error occurred in creating SDE Connection file: " + ex[0]
#self.log.error('Error occurred while creating connection file: ' + ex[0])
return False
# This is a sample of how to add data
# to an mxd file, convert to msd and publish
# to ArcGIS Server without using ArcMap.
# NOTE: This is just a sample.
# You'll need to change it up
# to work in your environment.
# YOU NEED AN EMPTY MXD FILE TO START (empty.mxd)
# As of right now, you can't create a new mxd from
# nothing in Arcpy.
# This is meant to be run directly on
# your ArcGIS Server machine.
import arcpy
from create_sde_connection import CreateSDEConnection
# Copy an empty mxd to a new mxd
# would be cooler if I could just create
# a new mxd to start with, but what you gonna do
mxd = arcpy.mapping.MapDocument(r"c:\temp\ags\empty.mxd")
mxd.saveACopy(r"c:\temp\ags\testing.mxd")
print mxd.filePath
del mxd
# create my SDE connection
# I have a little script to do it,
# but create it or use it however you want
sde = CreateSDEConnection("c:\\temp\\ags\\")
# make sure you can
if sde.createConnection() == True:
features = sde.connection + "\\FEATURES.SDE.DATA_Network_Classes\\FEATURES.SDE.ssFeature"
feature_Layer = "SDE.ssFeature_Layer"
lyr_file = r"c:\temp\ags\feature.lyr"
workspace = r"c:\temp\ags"
# use a where clause that works with your data
where_clause = "'NAME' <> ''"
arcpy.MakeFeatureLayer_management(features, feature_Layer, where_clause, workspace)
arcpy.SaveToLayerFile_management(feature_Layer, lyr_file, "ABSOLUTE")
# add the layer file to your map
mxd = arcpy.mapping.MapDocument(r"c:\temp\ags\testing.mxd")
print mxd.filePath
addLayer = arcpy.mapping.Layer(lyr_file)
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
mxd.saveACopy(r"c:\temp\ags\topublish.mxd")
del mxd
# convert to MSD
mxd = arcpy.mapping.MapDocument(r"c:\temp\ags\topublish.mxd")
msd = r"c:\temp\ags\demo.msd"
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.ConvertToMSD(mxd, msd, df, "NORMAL", "NORMAL")
# publish MSD
# use localhost if running directly
# in AGS machine. You should be, the point is
# you don't have ArcMap to publish data
server_url = "http://localhost/ArcGIS/rest/services"
server_name = "localhost"
service_name = "arcpydemo"
folder_name = "demo"
capabilities = ["WMS"]
user_name = "YOUR_NAME"
password = "YOUR_PASSWORD"
domain = "YOUR_DOMAIN"
arcpy.mapping.PublishMSDToServer(msd, server_url, server_name, service_name,
folder_name, capabilities, user_name, password, domain)
# clean up
del mxd, msd
print "Service published without issue"
# you hope, I didn't really add error checking
@kmilovm
Copy link

kmilovm commented Dec 22, 2016

Hi there,

Which toolbox must I import to get this working, because the method CreateArcSDEConnectionFile_management of arcpy was not found when I try to run it

thanks in advance

Daniel V.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment