Last active
April 26, 2019 21:06
-
-
Save nommuna2/8396cf870468ee352a2afabc076a77f6 to your computer and use it in GitHub Desktop.
(Arcpy and ArcGIS API for Python) Automation of sd files and then publishing them to ArcGIS Enterprise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcgis | |
from arcgis.gis import GIS | |
import arcpy | |
import os | |
def createSDFiles(): | |
workSpace = 'C:/sdFiles/' | |
arcpy.env.workspace = workSpace | |
fileList = os.listdir(workSpace) | |
try: | |
for item in fileList: | |
mxd = arcpy.mapping.MapDocument(workSpace + item) | |
sdDraftFilePath = '{}{}.sddraft'.format(workSpace, os.path.splitext(item)[0]) | |
sd = '{}{}.sd'.format(workSpace, os.path.splitext(item)[0]) | |
print('Creating sdDraft: {}'.format(sdDraftFilePath)) | |
sdDraft = arcpy.mapping.CreateMapSDDraft(map_document=mxd, out_sddraft=sdDraftFilePath, service_name=os.path.splitext(item)[0], server_type='MY_HOSTED_SERVICES', summary='ServiceData', tags='ServiceData') | |
print('Creating SD file: {}'.format(sd)) | |
arcpy.StageService_server(sdDraftFilePath,sd) | |
except Exception as e: | |
print(e) | |
def publishFeatureLayer(portalURL, username, password, workspace): | |
gis = GIS(portalURL, username, password, workspace) | |
dataDir = workspace | |
fileList = os.listdir(dataDir) | |
sdFileList = [item for item in fileList if item.endswith('.sd')] | |
print(sdFileList) | |
for currentFile in sdFileList: | |
item = gis.content.add({}, dataDir + currentFile) | |
print('Publishing item {}'.format(currentFile)) | |
publishedItem = item.publish() | |
createSDFiles() | |
publishFeatureLayer('portalURL','username', 'password', 'C:/sdFiles/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment