Correction on previous post: I attempted to run the publishing script again today from within my python IDE (pyscripter) and it executed correctly without having signed into ArcGIS Online manually through Desktop. I hadn't changed anything in my script from last week before executing it, so it's possible that logging out and back in on the system may account for it working now.However, I confirmed that I am still unable to get it to work though a scheduled task. I logged the error to a text file, and the following was recorded:Executing: UploadServiceDefinition C:\Workspace\KYEM_Test\KYEM_TEST3.sd "My Hosted Services" KYEM_TEST3 # FROM_SERVICE_DEFINITION # STARTED OVERRIDE_DEFINITION SHARE_ONLINE PRIVATE NO_SHARE_ORGANIZATION #
Start Time: Mon Sep 16 14:06:05 2013
Failed to execute. Parameters are not valid.
ERROR 000732: Server: Dataset My Hosted Services does not exist or is not supported
WARNING 001404: You are not signed in to ArcGIS Online.
Failed to execute (UploadServiceDefinition).
Failed at Mon Sep 16 14:06:05 2013 (Elapsed Time: 0.02 seconds)
The script (only minor changes from Esri's sample):
import arcpy, os, sys
from datetime import datetime
import xml.dom.minidom as DOM
arcpy.env.overwriteOutput = True
# Define global variables from User Input
mapDoc = 'KYEM.mxd'
serviceName = 'KYEM'
shareLevel = 'PRIVATE' # Options: PUBLIC or PRIVATE
shareOrg = 'NO_SHARE_ORGANIZATION' # Options: SHARE_ORGANIZATION and NO_SHARE_ORGANIZATION
shareGroups = '' # Options: Valid groups that user is member of
tempPath = sys.path[0]
#SignInToPortal function does not work in 10.2
#for 10.1: uncomment below line to enable sign in
#for 10.2: (1): Sign in to arcgis desktop and check 'sign in automatically'
# (2): Schedule task to run publishFS.py script, using same user as in step 1
##arcpy.SignInToPortal_server('','','http://www.arcgis.com/')
sdDraft = tempPath+'/{}.sddraft'.format(serviceName)
newSDdraft = 'updatedDraft.sddraft'
try:
os.remove(serviceName+'.sd')
print 'SD already exists, overwriting..'
SD = os.path.join(tempPath, serviceName + '.sd')
print 'File removed and overwritten'
except OSError:
print 'No SD exists, writing one now.'
SD = os.path.join(tempPath, serviceName + '.sd')
try:
# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sdDraft, serviceName, 'MY_HOSTED_SERVICES')
# Read the contents of the original SDDraft into an xml parser
doc = DOM.parse(sdDraft)
# The follow 5 code pieces modify the SDDraft from a new MapService
# with caching capabilities to a FeatureService with Query,Create,
# Update,Delete,Uploads,Editing capabilities. The first two code
# pieces handle overwriting an existing service. The last three pieces
# change Map to Feature Service, disable caching and set appropriate
# capabilities. You can customize the capabilities by removing items.
# Note you cannot disable Query from a Feature Service.
tagsType = doc.getElementsByTagName('Type')
for tagType in tagsType:
if tagType.parentNode.tagName == 'SVCManifest':
if tagType.hasChildNodes():
tagType.firstChild.data = 'esriServiceDefinitionType_Replacement'
tagsState = doc.getElementsByTagName('State')
for tagState in tagsState:
if tagState.parentNode.tagName == 'SVCManifest':
if tagState.hasChildNodes():
tagState.firstChild.data = 'esriSDState_Published'
# Change service type from map service to feature service
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
if typeName.firstChild.data == 'MapServer':
typeName.firstChild.data = 'FeatureServer'
#Turn off caching
configProps = doc.getElementsByTagName('ConfigurationProperties')[0]
propArray = configProps.firstChild
propSets = propArray.childNodes
for propSet in propSets:
keyValues = propSet.childNodes
for keyValue in keyValues:
if keyValue.tagName == 'Key':
if keyValue.firstChild.data == 'isCached':
keyValue.nextSibling.firstChild.data = 'false'
#Turn on feature access capabilities
configProps = doc.getElementsByTagName('Info')[0]
propArray = configProps.firstChild
propSets = propArray.childNodes
for propSet in propSets:
keyValues = propSet.childNodes
for keyValue in keyValues:
if keyValue.tagName == 'Key':
if keyValue.firstChild.data == 'WebCapabilities':
keyValue.nextSibling.firstChild.data = 'Query,Create,Update,Delete,Uploads,Editing'
# Write the new draft to disk
f = open(newSDdraft, 'w')
doc.writexml( f )
f.close()
# Analyze the service
analysis = arcpy.mapping.AnalyzeForSD(newSDdraft)
if analysis['errors'] == {}:
# Stage the service
arcpy.StageService_server(newSDdraft, SD)
# Upload the service. The OVERRIDE_DEFINITION parameter allows you to override the
# sharing properties set in the service definition with new values.
arcpy.UploadServiceDefinition_server(SD, 'My Hosted Services', serviceName, '', '', '', '', 'OVERRIDE_DEFINITION','SHARE_ONLINE', shareLevel, shareOrg, shareGroups)
print 'Uploaded and overwrote service'
# Write messages to a Text File
txtFile = open(tempPath+'/{}-log.txt'.format(serviceName),"a")
txtFile.write (str(datetime.now()) + " | " + "Uploaded and overwrote service" + "\n")
txtFile.close()
else:
# If the sddraft analysis contained errors, display them and quit.
print analysis['errors']
# Write messages to a Text File
txtFile = open(tempPath+'/{}-log.txt'.format(serviceName),"a")
txtFile.write (str(datetime.now()) + " | " + analysis['errors'] + "\n")
txtFile.close()
except:
print arcpy.GetMessages()
# Write messages to a Text File
txtFile = open(tempPath+'/{}-log.txt'.format(serviceName),"a")
txtFile.write (str(datetime.now()) + " | Last Chance Message:" + arcpy.GetMessages() + "\n")
txtFile.close()