1

I am trying to come up with a method that utilizes lyrx files to routinely overwrite a feature layer on ArcGIS Portal. I've tried a few methods that utilize Python scripts, ModelBuilder and Task Scheduler to accomplish this, but I feel like the main issue is with the file format (lyrx).

I am back at square one with this, so any feedback/suggestions?

aprx = arcpy.mp.ArcGISProject(aprx_path)
map = aprx.listMaps()[0]
layer = map.addDataFromPath(lyr_file_path)
outputfc_path = f"{outputgdb}/{outputfc_name}"
arcpy.FeatureClassToFeatureClass_conversion(in_features = layer, out_path = outputgdb, out_name = outputfc_name)
print(f"Feature class created at: {outputfc_path}")
        
gis = GIS(portalurl, username, password)
    item = gis.content.get(itemid)
    flayer_collection = FeatureLayerCollection.fromitem(item)
    flayer_collection.manager.overwrite(outputfc_path)
    try:
        flayer_collection.manager.overwrite(outputfc_path)
        print("Feature layer has been successfully overwritten with updated data.")
    except Exception as e:
        print(f"Error during overwrite: {e}")

All my variables are assigned in a different section which shows personal info so i cant include it in the post. This has worked the best in terms of making the lyr into a feature class. However, when uploading to portal it says that it has been modifeid, but the feature count is not what it should be!

3
  • Is there a reason that you are using a lyrx file as opposed to it's geodatabase version/shapefile version/ etc. Commented Aug 7, 2024 at 17:24
  • i meant to say lyr instead of lyrx, sorry! its just what i have been given and represents the most current updated form of that data, so i am restrained to only using lyr files to overwrite the layer. i did update my original post to include the code, i tried an approach where i make a copy of the lyr in the form of a feature class, and it works perfectly, but the overwrite is causing problems Commented Aug 8, 2024 at 15:13
  • 1
    A layer file (lyr/lyrx) doesn't contain any data. It only contains a link to the underlying datasource and may also include symbology, definition query, labelling amongst other properties. If someone has provided you with a layer file and not the underlying data, then you don't have everything you need. Commented Aug 12, 2024 at 9:22

1 Answer 1

0

You can use ArcPy to overwrite and avoid using the ArcGIS API for Python altogether. The basis of the code below are from a blog post here. Note: for hosted feature services only.

import arcpy
import os

################################################################################
## INPUT REQUIRED  #############################################################

## the path to the APRX that contains the Map with the layers to publish
aprx_path = r""

## the name of the Map in the APRX that contains the layers to publish
map_name = "MAP NAME"

## a folder to stage the definition files
staging_fldr = r""

## some tags for our Hosted Feature Service
tags = "tag1,tag2,tag3"

## a description for our Hosted Feature Service
description = "This is our description"

## a summary for our Hosted Feature Service
summary = "This is our summary"

## overwrite the current feature service
overwrite = True

################################################################################
## ACCESS ARCGIS PRO COMPONENTS  ###############################################

## access the APRX
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the correct map
m = aprx.listMaps(map_name)[0]


################################################################################
## STAGING #####################################################################

## the service definition filenames and output paths
sddraft_output_filepath = os.path.join(staging_fldr, f"{map_name}.sddraft")
sd_output_filepath = os.path.join(staging_fldr, f"{map_name}.sd")

## if either of the output files already exists, then delete
if arcpy.Exists(sddraft_output_filepath):
    arcpy.Delete_management(sddraft_output_filepath)

if arcpy.Exists(sd_output_filepath):
    arcpy.Delete_management(sd_output_filepath)


################################################################################
## MANIPULATE THE SD DRAFT ######################################################

## use the Map object getWebLayerSharingDraft method
## returns a FeatureSharingDraft object
sd_draft = m.getWebLayerSharingDraft(
    server_type = "HOSTING_SERVER",
    service_type = "FEATURE",
    service_name = map_name
)

## alter some properties of the sd_draft
sd_draft.description = description
sd_draft.summary = summary
sd_draft.tags = tags
sd_draft.allowExporting = True
sd_draft.overwriteExistingService = overwrite

## create the Service Definition Draft file
sd_draft.exportToSDDraft(sddraft_output_filepath)


################################################################################
## STAGE THE SERVICE ###########################################################

## stage the service
arcpy.server.StageService(
    in_service_definition_draft = sddraft_output_filepath,
    out_service_definition = sd_output_filepath
)


################################################################################
## PUBLISH TO ARGIS ONLINE #####################################################

## publish to agol
arcpy.server.UploadServiceDefinition(
    in_sd_file = sd_output_filepath,
    in_server = "HOSTING_SERVER"
)


################################################################################

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.