0

I am working on a ArcPy script *1 to update some existing layers that I have hosted on AGOL. I have been able to get everything running except for updating the summary, description and tags. The data is being fed into CreateWebLayerSDDraft() in the tags, description and summary parameters, but still nothing. EDIT: To clarify. This code runs without errors, but the Description, Tags and Summary are not updated

I tried to add another publish statement for hostedFS, but I kept getting a revolving door of errors *2. I think that it is either related to the way that I wrote the update() statement or something with the publish() statement. EDIT: These errors do not show up when I run the following script. They only show up when I try to publish() the Hosted Feature Service.

I am hoping to get this up and running, but I cannot for the life of me figure out what is going on.

Update() documentation

Publish() documentation

*1

    list_of_fc = [["MetroBusRoutes",
                   f"Current MetroBus Routes for the Metro Saint Louis Transit system as of {date}.",
                   f"Current MetroBus Routes for the Metro Saint Louis Transit system as of {date}. n\Sign: {sign}, n\Date Published: {date} n\Projection: WGS1984",
                   ["metro","routes","transit","metrobus","bus","MetroBusStops"],
                   ],


       for fc in list_of_fc:
        # Local paths to create temporary content
        print(f"Start of: {fc[0]}")
        relPath = sys.path[0]

        sddraft = os.path.join(relPath, "WebUpdate.sddraft")
        sd = os.path.join(relPath, "WebUpdate.sd")

        # Create a new SDDraft and stage to SD
        print("Creating SD file")
        ap.env.overwriteOutput = True
        prj = ap.mp.ArcGISProject(prjPath)
        mp = prj.listMaps(fc[0])[0]
        hosted_file_name = f"{fc[0]}_REGISTERED"
        ap.mp.CreateWebLayerSDDraft(map_or_layers=mp,
                                    out_sddraft=sddraft,
                                    service_name=hosted_file_name,
                                    server_type='HOSTING_SERVER',
                                    service_type='FEATURE_ACCESS',
                                    folder_name=folder,
                                    overwrite_existing_service=True,
                                    copy_data_to_server=True,
                                    summary=fc[1],
                                    description=fc[2],
                                    tags=fc[3])

        ap.StageService_server(sddraft, sd)

        print(f"Connecting to {portal}")

        gis = GIS(portal, user, password)  # user, password

        # Find the SD, update it, publish /w overwrite and set sharing and metadata
        print("Search for original SD on portal ...")

        print(f"Query: {hosted_file_name}")

        sdItem = gis.content.search(query=hosted_file_name,
                                    item_type="Service Definition")[0]
        hostedFS = gis.content.search(query=hosted_file_name,
                                    item_type="Feature Layer Collection")[0]

        # sdItem = gis.content.get(fc[4])

        print(' ')
        print("sdItem:")
        print(sdItem)
        print(f"Found SD: {sdItem.title}, ID: {sdItem.id} n Uploading and overwriting ...")

        print(' ')

        print("hostedFS:")
        print(hostedFS)
        print(f"Found SD: {hostedFS.title}, ID: {hostedFS.id} n Uploading and overwriting ...")
        print(' ')

        sdItem.update(data=sd)
        hostedFS.update(item_properties={'tags': fc[3],'description': fc[2]})

        print("Overwriting existing feature service ...")

        fs = sdItem.publish(overwrite=True)

        if shrOrg or shrEveryone or shrGroups:
            print("Setting sharing options ...")
            fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

        print(f"Finished updating: {fs.title} – ID: {fs.id}")

*2 There are 2 possible error codes that keep switching back and forth when I run this.

EDIT: The following errors only show up when I run a publish() with the hosted Feature Service.

hfs = hostedFS.publish(overwrite=True)
if shrOrg or shrEveryone or shrGroups:
    print("Setting sharing options ...")
    fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

print(f"Finished updating: {fs.title} – ID: {fs.id}")

#ERROR CODES
#ERROR 1
    Traceback (most recent call last):
Finished updating: MetroBusRoutes_REGISTERED – ID: 08a5bfc4fb974e94b03cfe24627cc43c
  File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 555, in <module>
    updatWebLayers(pattern_file_date)
  File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 230, in updatWebLayers
    fs = item.publish(overwrite=True)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8327, in publish
    raise ValueError("A file_type must be provide, data format not recognized")
ValueError: A file_type must be provide, data format not recognized

#ERROR 2
    User cant overwrite this service, using this data, as this data is already referring to another service.
Traceback (most recent call last):
  File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 555, in <module>
    updatWebLayers(pattern_file_date)
  File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 230, in updatWebLayers
    fs = item.publish(overwrite=True)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8490, in publish
    folder, buildInitialCache)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\portalpy.py", line 372, in publish_item
    resp = self.con.post(path, postdata, files)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py", line 1177, in post
    self._handle_json_error(resp_json['error'], errorcode)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py", line 1198, in _handle_json_error
    raise RuntimeError(errormessage)
RuntimeError: User cant overwrite this service, using this data, as this data is already referring to another service.
(Error Code: 500)
  • What is wrong when you use CreateWebLayerSDDraft?. The second error might be related with this note ArcGIS does not permit overwriting if you published multiple hosted feature layers from the same data item. from arcgis doc – cabesuon Jan 24 at 22:19
  • Nothing pops out as wrong, but it might be easier to debug if drop the loop code and just hardcode some first run values. Maybe that'll highlight any particular issues (like is the gis.content.search returning what you expect it to return) – KHibma Jan 27 at 17:04
  • I have attempted just hard coding in some different values for Summary, Description and Tags, but it still will not overwrite the values that are there. Do you think that this could be an issue related to the ArcPro Map? Maybe the values are stored there and are just just keep overwriting the values that I am putting in there? – AvidDabbler Jan 27 at 17:17
0

So the issue ended up being related to the project that is being referenced in the script. Since that map was where the initial publish was done the metadata overwriting the information that I had stipulated. So what I ended up doing is adding another update() function at the end to overwrite the map attributes.

The other error that ended up happening was there was a list of Service Definitions that the search() function returned. Some of my searches ended up with the wrong Service Definition at position [0]. So I ended up creating a while loop to cycle through the list search() returned.

    for fc in list_of_fc:
        # Local paths to create temporary content
        print(f"Start of: {fc[0]}")
        relPath = sys.path[0]

        sddraft = os.path.join(relPath, "WebUpdate.sddraft")
        sd = os.path.join(relPath, "WebUpdate.sd")

        # Create a new SDDraft and stage to SD
        print("Creating SD file")
        ap.env.overwriteOutput = True # allow overwrite???
        prj = ap.mp.ArcGISProject(prjPath) # define the arcpro project
        mp = prj.listMaps(fc[0])[0] # get the first map that matches the feature class then get the first object
        lay = mp.listLayers()[0] # get the first layer from the map object
        hosted_file_name = f"{fc[0]}_REGISTERED" # define the hosted_file_name. This is what it is named on AGOL/DataStore
        ap.mp.CreateWebLayerSDDraft(map_or_layers=lay,
                                    out_sddraft=sddraft,
                                    service_name=hosted_file_name,
                                    server_type='HOSTING_SERVER',
                                    service_type='FEATURE_ACCESS',
                                    folder_name=folder,
                                    overwrite_existing_service=True,
                                    copy_data_to_server=True) # define service definition parameters

        ap.StageService_server(sddraft, sd) # put the service definition parameters in staging

        print(f"Connecting to {portal}")

        gis = GIS(portal, user, password)  # connect to what portal you are interested in publishing to using variables user and password

        # Find the SD, update it, publish /w overwrite and set sharing and metadata
        print("Search for original SD on portal ...")
        print(f"Query: {hosted_file_name}")

        list = gis.content.search(query=hosted_file_name,
                                    item_type="Service Definition")

        # scan list of items to make sure they are an exact match to hosted_file_name
        # preform update() and publish() to "Service Definition"
        i = 0
        # cycle through the list that search() brought back to make sure that it is an exact match to hosted_file_name
        while list[i].title != hosted_file_name:
            i += 1

        print("")
        print('MATCH FOUND!!!')
        print(f'item[i].title = {list[i].title}, hosted_file_name = {hosted_file_name}')
        item = list[i] # define item based on above findings

        item.update(data=sd) # use sd to update hosted "Service Definition" file
        print("Overwriting existing feature service ...")

        fs = item.publish(overwrite=True) # publish "Service Definition"

        if shrOrg or shrEveryone or shrGroups: # update sharing groups
            print("Setting sharing options ...")
            fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

        print("Updating Metadata...")
        item.update(item_properties={'snippet': fc[1], 'tags': fc[3],'description': fc[2]})

        print(f"Finished updating: {fs.title} – ID: {fs.id}")
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.