<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Update Service Definition on AGOL in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843980#M3623</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Could you post what the final code looks like? Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 28 Jan 2019 21:46:13 GMT</pubDate>
    <dc:creator>JacobMattison</dc:creator>
    <dc:date>2019-01-28T21:46:13Z</dc:date>
    <item>
      <title>Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843975#M3618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN&gt;Using the script at this blog: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fblogs.esri.com%2Fesri%2Farcgis%2F2017%2F03%2F14%2Fupdating-your-hosted-feature-services-with-arcgis-pro-and-the-arcgis-api-for-python%2F" rel="nofollow" target="_blank"&gt;https://blogs.esri.com/esri/arcgis/2017/03/14/updating-your-hosted-feature-services-with-arcgis-pro-and-the-arcgis-api-for-python/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Line 41: &lt;STRONG&gt;sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]&lt;/STRONG&gt;&lt;BR /&gt;returns the wrong Service Definition (SD). The title query is not strict enough, so it returns any SD with a "Z" in the title. The SD I'm looking for is Zoning.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Title or owner can be replaced with id:&lt;BR /&gt;&lt;STRONG&gt;sdItem = gis.content.search(query="title:"+ sd_fs_name + " AND id:" + serviceItemID, item_type= "Service Definition")[0]&lt;/STRONG&gt;&lt;BR /&gt;which throws: &lt;EM&gt;IndexError: list index out of range&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Many permutations have been tried for using ID.&lt;/P&gt;&lt;P&gt;If item_type= "Service Definition" is removed, line 41 will run:&lt;BR /&gt;&lt;STRONG&gt;sdItem = gis.content.search(query="title:"+ sd_fs_name + " AND id:" + serviceItemID)[0]&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;However, then on line 45:&lt;STRONG&gt; fs = sdItem.publish(overwrite=True)&lt;/STRONG&gt;&lt;BR /&gt;it throws:&lt;BR /&gt;&lt;EM&gt;File “C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis.py”, line 3125, in publish&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;if fileType == ‘shapefile’:&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;UnboundLocalError: local variable ‘fileType’ referenced before assignment&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, the question is - how do I specify the ID, and also specify the item type?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 05 Jan 2018 21:59:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843975#M3618</guid>
      <dc:creator>InyoCounty</dc:creator>
      <dc:date>2018-01-05T21:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843976#M3619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A query like the following should work&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;type:"Service Definition"&amp;nbsp;&lt;SPAN style="background-color: #f6f6f6;"&gt;id:b7ec249e81b7405bd5322adee43d8feb&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;There isn't really a need to specify the type and the id though, since the item id alone is unique.&lt;/P&gt;&lt;P&gt;The following both return the same item.&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;sdItem&amp;nbsp;= gis.content.search('type:"Service Definition" id:&lt;SPAN style="background-color: #f6f6f6;"&gt;b7ec249e81b7405bd5322adee43d8feb&lt;/SPAN&gt;')[0]&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;&lt;SPAN style="background-color: #f6f6f6;"&gt;sdItem&amp;nbsp;= gis.content.search('id:&lt;SPAN&gt;b7ec249e81b7405bd5322adee43d8feb&lt;/SPAN&gt;')[0]&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 06 Jan 2018 03:22:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843976#M3619</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2018-01-06T03:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843977#M3620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Aaron,&lt;/P&gt;&lt;P&gt;There's still the problem that the line&amp;nbsp;&lt;EM&gt;gis.content.search&lt;/EM&gt; won't run if type is specified, but if type is not specified then the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;sdItem.publish&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;errors out.&lt;/P&gt;&lt;P&gt;Here is the error for including both type and id, as formatted in your response, with the one difference of passing in the id as a variable:&lt;/P&gt;&lt;P&gt;---------------------------------------------------&lt;/P&gt;&lt;P&gt;pydev debugger: starting (pid: 11304)&lt;BR /&gt;Creating SD file&lt;BR /&gt;&lt;SPAN&gt;Connecting to &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fwww.arcgis.com" rel="nofollow" target="_blank"&gt;http://www.arcgis.com&lt;/A&gt;&lt;BR /&gt;Search for original SD on portal...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt; File "C:\GIS\PROJECTS\PLANNING\UpdateZoning&amp;amp;GPToMatchNewParcels\updateAGOL_Pro.py", line 109, in &amp;lt;module&amp;gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; update("Zoning", "f78c18c992e74846806fc7dc98fb66f8")&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; File "C:\GIS\PROJECTS\PLANNING\UpdateZoning&amp;amp;GPToMatchNewParcels\updateAGOL_Pro.py", line 76, in update&lt;/EM&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt; sdItem = gis.content.search('type:"Service Definition" id:' + serviceItemID)[0]&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt;IndexError: list index out of range&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Here is the error when fileType is not specified:&lt;/P&gt;&lt;P&gt;------------------------------------------------------------------------&lt;BR /&gt;pydev debugger: starting (pid: 7432)&lt;BR /&gt;Creating SD file&lt;BR /&gt;&lt;SPAN&gt;Connecting to &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fwww.arcgis.com" rel="nofollow" target="_blank"&gt;http://www.arcgis.com&lt;/A&gt;&lt;BR /&gt;Search for original SD on portal...&lt;BR /&gt;Found SD: Zoning, ID: f78c18c992e74846806fc7dc98fb66f8 &lt;BR /&gt; Uploading and overwriting...&lt;BR /&gt;Overwriting existing feature service...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt; File "C:\GIS\PROJECTS\PLANNING\UpdateZoning&amp;amp;GPToMatchNewParcels\updateAGOL_Pro.py", line 110, in &amp;lt;module&amp;gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; update("Zoning", "f78c18c992e74846806fc7dc98fb66f8")&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; File "C:\GIS\PROJECTS\PLANNING\UpdateZoning&amp;amp;GPToMatchNewParcels\updateAGOL_Pro.py", line 101, in update&lt;/EM&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt; fs = sdItem.publish(overwrite=True)&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt; File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis.py", line 3125, in publish&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt; if fileType == 'shapefile':&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt;UnboundLocalError: local variable 'fileType' referenced before assignment&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 Jan 2018 17:19:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843977#M3620</guid>
      <dc:creator>InyoCounty</dc:creator>
      <dc:date>2018-01-08T17:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843978#M3621</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;Mike Hay wrote:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks Aaron,&lt;/P&gt;&lt;P&gt;There's still the problem that the line&amp;nbsp;&lt;EM&gt;gis.content.search&lt;/EM&gt; won't run if type is specified, but if type is not specified then the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;sdItem.publish&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;errors out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It sounds like the query you are creating is returning no items. The search function returns a list of items. Since no items are returned, and you are accessing it via an index of 0, you get the "list index out of range error".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The itemId you need to specify should be the item id of the service definition file on AGOL, not the id of the published service. Can you verify the item id points to a service definition file on AGOL?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 Jan 2018 17:57:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843978#M3621</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2018-01-08T17:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843979#M3622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That was it! The ID that was being passed in was for the feature layer, not the service definition. Thanks Aaron!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 Jan 2018 18:17:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843979#M3622</guid>
      <dc:creator>InyoCounty</dc:creator>
      <dc:date>2018-01-08T18:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843980#M3623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Could you post what the final code looks like? Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jan 2019 21:46:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843980#M3623</guid>
      <dc:creator>JacobMattison</dc:creator>
      <dc:date>2019-01-28T21:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: Update Service Definition on AGOL</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843981#M3624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os, sys&lt;/P&gt;&lt;P&gt;from arcgis.gis import GIS&lt;/P&gt;&lt;P&gt;def update(planningLayer, serviceItemID):&lt;BR /&gt;&lt;BR /&gt; # Set the path to the project&lt;BR /&gt; prjPath = r"\\server\ServiceProjects\Planning\\" + planningLayer + ".aprx"&lt;BR /&gt; &lt;BR /&gt;# Feature service/SD name in arcgis.com, user/password of the owner account&lt;BR /&gt; sd_fs_name = planningLayer&lt;BR /&gt; portal = "http://www.arcgis.com" # Can also reference a local portal&lt;BR /&gt; user = ""&lt;BR /&gt; password = ""&lt;BR /&gt; &lt;BR /&gt; # Set sharing options&lt;BR /&gt; shrOrg = True&lt;BR /&gt; shrEveryone = True&lt;BR /&gt; shrGroups = ""&lt;BR /&gt; &lt;BR /&gt;# Local paths to create temporary content&lt;BR /&gt; relPath = sys.path[0]&lt;BR /&gt; sddraft = os.path.join(relPath, planningLayer + ".sddraft")&lt;BR /&gt; sd = os.path.join(relPath, planningLayer + ".sd")&lt;BR /&gt; &lt;BR /&gt; # Create a new SDDraft and stage to SD&lt;BR /&gt; print("Creating SD file")&lt;BR /&gt; arcpy.env.overwriteOutput = True&lt;BR /&gt; prj = arcpy.mp.ArcGISProject(prjPath)&lt;BR /&gt; mp = prj.listMaps()[0]&lt;BR /&gt; arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)&lt;BR /&gt; arcpy.StageService_server(sddraft, sd)&lt;BR /&gt; &lt;BR /&gt; print("Connecting to {}".format(portal))&lt;BR /&gt; gis = GIS(portal, user, password)&lt;BR /&gt; &lt;BR /&gt; # Find the SD, update it, publish /w overwrite and set sharing and metadata&lt;BR /&gt; print("Search for original SD on portal...")&lt;BR /&gt; &lt;BR /&gt; sdItem = gis.content.search('id:' + serviceItemID)[0]&lt;/P&gt;&lt;P&gt;print("Found SD: {}, ID: {} \n Uploading and overwriting...".format(sdItem.title, sdItem.id))&lt;BR /&gt; #print("Found SD: {}, ID: {} \n Uploading and overwriting...".format(sdItem.id))&lt;BR /&gt; sdItem.update(data=sd)&lt;BR /&gt; print("Overwriting existing feature service...")&lt;BR /&gt; fs = sdItem.publish(overwrite=True)&lt;BR /&gt; &lt;BR /&gt; if shrOrg or shrEveryone or shrGroups:&lt;BR /&gt; print("Setting sharing options...")&lt;BR /&gt; fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)&lt;BR /&gt; &lt;BR /&gt; print("Finished updating: {} - ID: {}".format(fs.title, fs.id))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jan 2019 22:11:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/update-service-definition-on-agol/m-p/843981#M3624</guid>
      <dc:creator>InyoCounty</dc:creator>
      <dc:date>2019-01-28T22:11:28Z</dc:date>
    </item>
  </channel>
</rss>

