<SPAN class="comment token"># ---------------------------------------------------------------------------</SPAN>
<SPAN class="comment token"># Script: Creating an Service Definition File and Overwritting Existing Feature Service on ArcGIS Online</SPAN>
<SPAN class="comment token"># Description: This script does the following:</SPAN>
<SPAN class="comment token"># (1) Sets up the variables</SPAN>
<SPAN class="comment token"># (2) Creates an SD File</SPAN>
<SPAN class="comment token"># (3) Overwrites Existing Feature Service</SPAN>
<SPAN class="comment token">#</SPAN>
<SPAN class="comment token"># Date: 9/12/2017</SPAN>
<SPAN class="comment token"># </SPAN>
<SPAN class="comment token"># ---------------------------------------------------------------------------</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os<SPAN class="punctuation token">,</SPAN> sys<SPAN class="punctuation token">,</SPAN> string<SPAN class="punctuation token">,</SPAN> time
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS
<SPAN class="comment token"># Set the date.</SPAN>
Date <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%m-%d-%Y"</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Set the time.</SPAN>
Time <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%I:%M:%S %p"</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<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">"Process started at "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Date<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Time<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"."</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">### Start setting variables</SPAN>
<SPAN class="comment token"># Set the path to the project</SPAN>
prjPath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"J:\ENGSYS\GIS_Testing\ArcGISOnline_PublishingSrvc\TestScript.aprx"</SPAN>
<SPAN class="comment token"># Update the following variables to match:</SPAN>
<SPAN class="comment token"># Feature service/SD name in arcgis.com, user/password of the owner account</SPAN>
sd_fs_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"TestDocumentPortal_WFL1"</SPAN>
portal <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2FXXXX.maps.arcgis.com" target="_blank">https://xyz.maps.arcgis.com</A><SPAN>"</SPAN></SPAN> <SPAN class="comment token"># Can also reference a local portal</SPAN>
user <SPAN class="operator token">=</SPAN> <SPAN class="string token">"xyz"</SPAN>
password <SPAN class="operator token">=</SPAN> <SPAN class="string token">"xyz"</SPAN>
<SPAN class="comment token"># Set sharing options</SPAN>
shrOrg <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
shrEveryone <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN>
shrGroups <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
<SPAN class="comment token">### End setting variables</SPAN>
<SPAN class="comment token"># Local paths to create temporary content</SPAN>
relPath <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
sddraft <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>relPath<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WebUpdate.sddraft"</SPAN><SPAN class="punctuation token">)</SPAN>
sd <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>relPath<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WebUpdate.sd"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Create a new SDDraft and stage to SD</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Creating SD file"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
prj <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN>prjPath<SPAN class="punctuation token">)</SPAN>
mp <SPAN class="operator token">=</SPAN> prj<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>CreateWebLayerSDDraft<SPAN class="punctuation token">(</SPAN>mp<SPAN class="punctuation token">,</SPAN> sddraft<SPAN class="punctuation token">,</SPAN> sd_fs_name<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MY_HOSTED_SERVICES'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FEATURE_ACCESS'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>StageService_server<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">,</SPAN> sd<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Connecting to {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>portal<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>portal<SPAN class="punctuation token">,</SPAN> user<SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Find the SD, update it, publish /w overwrite and set sharing and metadata</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Search for original SD on portal..."</SPAN><SPAN class="punctuation token">)</SPAN>
sdItem <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{} AND owner:{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>sd_fs_name<SPAN class="punctuation token">,</SPAN> user<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> item_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"Service Definition"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Found SD: {}, ID: {} \n Uploading and overwriting..."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>sdItem<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">,</SPAN> sdItem<SPAN class="punctuation token">.</SPAN>id<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
sdItem<SPAN class="punctuation token">.</SPAN>update<SPAN class="punctuation token">(</SPAN>data<SPAN class="operator token">=</SPAN>sd<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Overwriting existing feature service..."</SPAN><SPAN class="punctuation token">)</SPAN>
fs <SPAN class="operator token">=</SPAN> sdItem<SPAN class="punctuation token">.</SPAN>publish<SPAN class="punctuation token">(</SPAN>overwrite<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> shrOrg <SPAN class="operator token">or</SPAN> shrEveryone <SPAN class="operator token">or</SPAN> shrGroups<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Setting sharing options..."</SPAN><SPAN class="punctuation token">)</SPAN>
fs<SPAN class="punctuation token">.</SPAN>share<SPAN class="punctuation token">(</SPAN>org<SPAN class="operator token">=</SPAN>shrOrg<SPAN class="punctuation token">,</SPAN> everyone<SPAN class="operator token">=</SPAN>shrEveryone<SPAN class="punctuation token">,</SPAN> groups<SPAN class="operator token">=</SPAN>shrGroups<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Finished updating: {} - ID: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>fs<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">,</SPAN> fs<SPAN class="punctuation token">.</SPAN>id<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">"Process completed at "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Date<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Time<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"."</SPAN><SPAN class="punctuation 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>My script above runs successfully.
I am trying to add to it by emailing a specified user if it fails. I have read multiple samples, but have not gotten one to work. Anyone have something a novice python user could use?