I've created a model in ArcGIS Pro ModelBuilder which attempts to carry out the following:
Then using the code in the following resource:
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:
Below are the parameters I used as per the Esri article:
Any suggestions/assistance would be gratefully received.
Thanks
Andy
Solved! Go to Solution.
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.
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:
* C:\ArcGIS\Server\framework\runtime\ArcGIS\bin\Python\Scripts\proenv.bat
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.
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!