Select to view content in your preferred language

Error publishing feature layer using ModelBuilder in ArcGIS Pro

786
3
Jump to solution
10-09-2023 09:48 AM
Labels (3)
AndrewFerguson2
New Contributor

I've created a model in ArcGIS Pro ModelBuilder which attempts to carry out the following:

  1. Variable: Database connection (sql server)
  2. Make Query Layer (input = Database Connection, output = "Dwellings SQL")
  3. Make Feature Layer (input = "Dwellings SQL", output = "Dwellings")

Then using the code in the following resource:

https://www.esri.com/arcgis-blog/products/arcgis-pro/mapping/publish-and-overwrite-web-layers-in-mod...

I created a toolbox which should publish (overwrite) a layer called "Dwellings" in our AGOL account. I have tweaked the code in the ScriptTool function as such:

def ScriptTool(map, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir):
"""ScriptTool function docstring"""

# Set output file names
sddraft_filename = service + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps(map)[0]
selected_layer = m.listLayers()[0]

# Create FeatureSharingDraft and set service properties
sharing_draft = m.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", service, [selected_layer])
sharing_draft.summary = summary
sharing_draft.tags = tags
sharing_draft.description = description
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"
sharing_draft.overwriteExistingService = overwriteService

# Create Service Definition Draft file
sharing_draft.exportToSDDraft(sddraft_output_filename)
outsddraft = sddraft_output_filename
arcpy.AddMessage("Service definition draft created")

# Modify capabilities
if enableEditing or enableSync:
    ModifyCapabilities(sddraft_output_filename, enableEditing, enableSync)
if enableWFS:
    EnableWFS(sddraft_output_filename)

# Set time zone
if(timezone != ""):
    property_set = [{
        "key": "dateFieldsRespectsDayLightSavingTime",
        "value": "true"
    },
        {
            "key": "dateFieldsTimezoneID",
            "value": timezone
        }]
    SetTimezone(sddraft_output_filename, property_set=property_set)

# Create Service Definition file
sd_filename = service + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
arcpy.StageService_server(sddraft_output_filename, sd_output_filename)
arcpy.AddMessage("Service definition created")

# Upload to portal
output = arcpy.UploadServiceDefinition_server(sd_output_filename, "My Hosted Services", in_override="OVERRIDE_DEFINITION", in_public=share_public, in_organization=share_organization, in_groups=share_groups)
arcpy.AddMessage("Service published")

return output[5]

...so that the function will publish a feature layer rather than the whole map. On running the code I receive the following error:

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\server.py", line 1179, in StageService raise e File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\server.py", line 1176, in StageService retval = convertArcObjectToPythonObject(gp.StageService_server(*gp_fixargs((in_service_definition_draft, out_service_definition, staging_version), True))) File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing_base.py", line 512, in return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: ERROR 001272: Analyzer errors were encountered ([{"code":"00102","message":"Selected layer does not contain a required layer type for web feature layer","object":"Map"}]). Failed to execute (StageService).

Failed script Publish Web Feature Layer... Failed to execute (Publish Web Feature Layer).

I can't understand why this is happening as the current map does display the imported feature layer and when running the section of the code under #Reference map to publish in the python console it does find the "Dwellings" feature layer. The line of code which throws the error is:

arcpy.StageService_server(sddraft_output_filename, sd_output_filename)

...and the error message isn't very helpful, I can't find any helpful documentation on Esri's website which details if the problem is with the feature layer or a bug in the code.

I am using ArcGIS Pro 3.1.3 with the default arcgispro-py3 environment.

Below is a diagram of my model:

8aSu8.png

 

Below are the parameters I used as per the Esri article:

nzaIM.png

Any suggestions/assistance would be gratefully received.

 

Thanks

Andy

1 Solution

Accepted Solutions
AndrewFerguson2
New Contributor

Hi Max.

I've now got a working solution. I ended up exporting the model as a .py file so that I could attempt to debug the code.

Firstly I changed the "arcpy.management.MakeFeatureLayer()" function to "arcpy.FeatureClassToFeatureClass_conversion" and provided the query layer to that as a parameter. This still seemed to throw the same error.

I then tried it again after saving the project file and it worked.

It would appear that the Analyzer was throwing a wobble because although arcpy was able to select the layers currently visible on the opened map, it wasn't able to publish any because the project file wasn't saved. I added the:

aprx.save()

...command into the ScriptTool() function ("aprx" is a variable representing the project file) and this seemed to resolve the issue (it would be handy for this to have been detailed in the error message returned by the Analyzer).

Not sure if this solution can be applied to your issue as well but do let me know if it helps and thank you for your reply.

View solution in original post

3 Replies
maxsquires2
New Contributor II

Hi Andrew,

I am having a similar problem and thought I'd share my experience in case we can find some common issue.

The key here seems to be the enabling of 'Feature Service' as it relates to the specific table I'm dealing with.

Do you have this problem with other data in your database or just the `Dwellings` feature layer?

I believe this is, in fact, related to the data set in question, not because the error indicates it but because when I publish any of my other 61 map services with other data in them I am not getting this error.

Some other things that I can add are:

  • When I perform the same exact process in the ArcGIS Pro 'Share' 'web layer' interface I get no analyzer errors.
  • If I publish as a Map Image Layer (without feature access enabled), using my script I get no analyzer errors. and the service is successfully published.
  • On the machine where this code is deployed from (the server*) 
    • arcpy.GetInstallInfo()["Version"] == '11.1'
  • On my workstation where i run ArcGIS Pro (in the Analysis/Python/Python Window)
    • arcpy.GetInstallInfo()["Version"] == '3.1.2'


* C:\ArcGIS\Server\framework\runtime\ArcGIS\bin\Python\Scripts\proenv.bat

AndrewFerguson2
New Contributor

Hi Max.

I've now got a working solution. I ended up exporting the model as a .py file so that I could attempt to debug the code.

Firstly I changed the "arcpy.management.MakeFeatureLayer()" function to "arcpy.FeatureClassToFeatureClass_conversion" and provided the query layer to that as a parameter. This still seemed to throw the same error.

I then tried it again after saving the project file and it worked.

It would appear that the Analyzer was throwing a wobble because although arcpy was able to select the layers currently visible on the opened map, it wasn't able to publish any because the project file wasn't saved. I added the:

aprx.save()

...command into the ScriptTool() function ("aprx" is a variable representing the project file) and this seemed to resolve the issue (it would be handy for this to have been detailed in the error message returned by the Analyzer).

Not sure if this solution can be applied to your issue as well but do let me know if it helps and thank you for your reply.

CassHonebein2
New Contributor II

Andrew,

Your solution worked for me and has put an end to several hours of banging my head against the wall. Adding aprx.save() before m.getWebLayerSharingDraft seems to have worked for me. 

Thank you!

0 Kudos