import arcpy, os import xml.dom.minidom as DOM print "Establishing environment" workspace = r"C:\temp\enableNA" mxd = arcpy.mapping.MapDocument(os.path.join(workspace,"enablenatest.mxd")) svrconnection = os.path.join(workspace, "svrconnection.ags") outsddraft = os.path.join(workspace,"service.sddraft") draftfinal = os.path.join(workspace,"draftfinal.sddraft") outsd = os.path.join(workspace,"naservice.sd") print "Creating initial draft" arcpy.mapping.CreateMapSDDraft(mxd, outsddraft, "PythonNaTest","ARCGIS_SERVER",svrconnection) soe = 'NAServer' print "Enabling Network Analysis in the draft" # Read the sddraft xml. doc = DOM.parse(outsddraft) # Find all elements named TypeName. This is where the server object extension (SOE) names are defined. typeNames = doc.getElementsByTagName('TypeName') for typeName in typeNames: # Get the TypeName whose properties we want to modify. if typeName.firstChild.data == soe: extension = typeName.parentNode for extElement in extension.childNodes: # Enabled SOE. if extElement.tagName == 'Enabled': extElement.firstChild.data = 'true' # Output to a new sddraft. f = open(draftfinal, 'w') doc.writexml( f ) f.close() print "Staging Service" arcpy.StageService_server(draftfinal, outsd) print "Publishing to Server" arcpy.UploadServiceDefinition_server(outsd,svrconnection,"PythonNATest")
I am trying to accomplish same thing like OP but I am getting an error after analyzing the modified sddraft file. I checked the modified sddraft and it is updated to "true". I can publish the mxd file using ArcMap with NA on. The error message is:
----ERRORS---The Network Analyst extension needed for layer "Route" is not enabled (CODE 146) applies to: Route
Dennis Jarrard, any help any help would be appreciated.
<SPAN class="comment token"># https://enterprise.arcgis.com/en/server/10.4/administer/windows/example-publish-a-map-service-from-a-map-document-mxd-.htm</SPAN> <SPAN class="comment token"># https://community.esri.com/thread/179472</SPAN> <SPAN class="comment token"># http://desktop.arcgis.com/en/arcmap/10.4/analyze/arcpy-mapping/createmapsddraft.htm</SPAN> <SPAN class="comment token"># https://community.esri.com/thread/85441</SPAN> <SPAN class="comment token"># A connection to ArcGIS Server must be established in the</SPAN> <SPAN class="comment token"># Catalog window of ArcMap before running this script</SPAN> <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">import</SPAN> urllib<SPAN class="punctuation token">,</SPAN> urllib2 <SPAN class="keyword token">import</SPAN> httplib <SPAN class="keyword token">import</SPAN> json <SPAN class="keyword token">import</SPAN> sys <SPAN class="keyword token">import</SPAN> xml<SPAN class="punctuation token">.</SPAN>dom<SPAN class="punctuation token">.</SPAN>minidom <SPAN class="keyword token">as</SPAN> DOM <SPAN class="comment token"># A function to generate a token given username, password and the adminURL.</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">getToken</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Token URL is typically http://server[:port]/arcgis/admin/generateToken</SPAN> tokenURL <SPAN class="operator token">=</SPAN> <SPAN class="string token">"/arcgis/admin/generateToken"</SPAN> params <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>urlencode<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> portalAdminName<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> portalAdminPassword<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'client'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'requestip'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'f'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'json'</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN> headers <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Content-type"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"application/x-www-form-urlencoded"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Accept"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"text/plain"</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># Connect to URL and post parameters</SPAN> httpConn <SPAN class="operator token">=</SPAN> httplib<SPAN class="punctuation token">.</SPAN>HTTPConnection<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">)</SPAN> httpConn<SPAN class="punctuation token">.</SPAN>request<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"POST"</SPAN><SPAN class="punctuation token">,</SPAN> tokenURL<SPAN class="punctuation token">,</SPAN> params<SPAN class="punctuation token">,</SPAN> headers<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Read response</SPAN> response <SPAN class="operator token">=</SPAN> httpConn<SPAN class="punctuation token">.</SPAN>getresponse<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> response<SPAN class="punctuation token">.</SPAN>status <SPAN class="operator token">!=</SPAN> <SPAN class="number token">200</SPAN><SPAN class="punctuation token">:</SPAN> httpConn<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Error while fetching tokens from admin URL. Please check the URL and try again."</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> data <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> httpConn<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Extract the token from it</SPAN> token <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>data<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> token<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">!=</SPAN> <SPAN class="number token">9</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> len<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Parameters are missing or too many, check your parameters."</SPAN><SPAN class="punctuation token">)</SPAN> sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> mxd <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> severadmurl <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> portalAdminName <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> portalAdminPassword <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN> serverName <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">]</SPAN> con <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">]</SPAN> severfolder <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">]</SPAN> maxRecordCount <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">]</SPAN> schemaLockingEnabled <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">]</SPAN> serverPort <SPAN class="operator token">=</SPAN> <SPAN class="number token">6080</SPAN> <SPAN class="comment token"># Define local variables</SPAN> wrkspc <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> wrkspc arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="comment token"># Generate Token</SPAN> token <SPAN class="operator token">=</SPAN> getToken<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" \nProcessing {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> mapDoc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Provide other service details</SPAN> service <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"."</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># service name</SPAN> sddraft <SPAN class="operator token">=</SPAN> wrkspc <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> service <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.sddraft'</SPAN> draftfinal <SPAN class="operator token">=</SPAN> wrkspc <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> service <SPAN class="operator token">+</SPAN> <SPAN class="string token">'draft.sddraft'</SPAN> sd <SPAN class="operator token">=</SPAN> wrkspc <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> service <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.sd'</SPAN> summary <SPAN class="operator token">=</SPAN> mapDoc<SPAN class="punctuation token">.</SPAN>description tags <SPAN class="operator token">=</SPAN> mapDoc<SPAN class="punctuation token">.</SPAN>tags <SPAN class="comment token"># Create service definition draft</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>CreateMapSDDraft<SPAN class="punctuation token">(</SPAN>mapDoc<SPAN class="punctuation token">,</SPAN> sddraft<SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ARCGIS_SERVER'</SPAN><SPAN class="punctuation token">,</SPAN> con<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> severfolder<SPAN class="punctuation token">,</SPAN> summary<SPAN class="punctuation token">,</SPAN> tags<SPAN class="punctuation token">)</SPAN> naserver <SPAN class="operator token">=</SPAN> <SPAN class="string token">'NAServer'</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Enabling Network Analysis in the draft"</SPAN> doc <SPAN class="operator token">=</SPAN> DOM<SPAN class="punctuation token">.</SPAN>parse<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Find all elements named TypeName. This is where the server object extension (SOE) names are defined.</SPAN> typeNames <SPAN class="operator token">=</SPAN> doc<SPAN class="punctuation token">.</SPAN>getElementsByTagName<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'TypeName'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> typeName <SPAN class="keyword token">in</SPAN> typeNames<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Get the TypeName whose properties we want to modify.</SPAN> <SPAN class="keyword token">if</SPAN> typeName<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">==</SPAN> naserver<SPAN class="punctuation token">:</SPAN> extension <SPAN class="operator token">=</SPAN> typeName<SPAN class="punctuation token">.</SPAN>parentNode <SPAN class="keyword token">for</SPAN> extElement <SPAN class="keyword token">in</SPAN> extension<SPAN class="punctuation token">.</SPAN>childNodes<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Enabled SOE.</SPAN> <SPAN class="keyword token">if</SPAN> extElement<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Enabled'</SPAN><SPAN class="punctuation token">:</SPAN> extElement<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> <SPAN class="string token">'true'</SPAN> <SPAN class="comment token"># Output to a new sddraft.</SPAN> f <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>draftfinal<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN> doc<SPAN class="punctuation token">.</SPAN>writexml<SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">)</SPAN> f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Analyze the service definition draft</SPAN> analysis <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>AnalyzeForSD<SPAN class="punctuation token">(</SPAN>draftfinal<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Print errors, warnings, and messages returned from the analysis</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"The following information was returned during analysis of the MXD:"</SPAN> <SPAN class="keyword token">for</SPAN> key <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'warnings'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'----'</SPAN> <SPAN class="operator token">+</SPAN> key<SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'---'</SPAN> vars <SPAN class="operator token">=</SPAN> analysis<SPAN class="punctuation token">[</SPAN>key<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>message<SPAN class="punctuation token">,</SPAN> code<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> layerlist<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> vars<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">' '</SPAN><SPAN class="punctuation token">,</SPAN> message<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">' (CODE %i)'</SPAN> <SPAN class="operator token">%</SPAN> code <SPAN class="keyword token">print</SPAN> <SPAN class="string token">' applies to:'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">for</SPAN> layer <SPAN class="keyword token">in</SPAN> layerlist<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.