0

I am trying to create an empty Hosted Feature service from my app using the ArcGIS REST API. I want that service to have multiple layers, and the fields would be dynamically created at creation type.

One would think that the createService API call (https://developers.arcgis.com/rest/users-groups-and-items/create-service/) would be the right thing to look at, right? Unfortunately, it looks like ESRI wants only the admins to do this. The documentation says: "This endpoint is only accessible to the content owner and members who are assigned the default administrator role. All other members will receive a permissions error when attempting to access this endpoint."

So then I looked into how ArcGIS Online does it and looks like they use a combination of /addItem (https://developers.arcgis.com/rest/users-groups-and-items/add-item/) and /publish (https://developers.arcgis.com/rest/users-groups-and-items/publish-item/) to achieve this.

To achieve what I need, I would have to first create a file GDB with my required layers, upload that, and then publish it. And this would mean that an uploaded file geodatabase is left as a file item, which just feels icky.

Is there a better way to do what I need to do?

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jul 24 at 10:02
1
  • Not in a position to test Online, but Enterprise allows a member in the publisher role to call createService and then carry on with addToDefinition, update and updateItems as needed. (Open browser dev tools and follow the flow while logged in as a publisher using Content > New Item > Feature Layer) Commented Jul 24 at 14:44

2 Answers 2

1

I agree the API reference isn't great at explaining the whole workflow. Let me try to help.

This endpoint is only accessible to the content owner and members who are assigned the default administrator role

To use that endpoint, you need to use a token created with permissions to create hosted content (like an editor user type in ArcGIS Online).

I have done that by creating the service schema manually (sending a JSON schema definition).

So then I looked into how ArcGIS Online does it and looks like they use a combination of /addItem (https://developers.arcgis.com/rest/users-groups-and-items/add-item/) and /publish (https://developers.arcgis.com/rest/users-groups-and-items/publish-item/) to achieve this.

Some time ago, I created this Postman collection to demo the whole workflow. I would be something like:

  1. Check that the service doesn't already exist (REST call)
  2. Create the item targetType=featureService (e.g., REST call)
  3. Add the schema definition to the service (e.g., REST call creating a point layer)

You can also check the Define a new feature layer tutorial included in the Portal and data services guide.

Update: I just a blog article that explains the different approaches to create hosted feature services among other things.

I hope this helps!

answered Sep 5 at 15:39
1
0

You don't need to upload an fGDB first. Even if you do, you can delete the fGDB from AGOL in code.

Here's how I publish (overwrite) without uploading an fGDB:

 arcpy.mp.CreateWebLayerSDDraft(mp, sddraft_file, map_name, overwrite_existing_service=True)
 print(4*" " + f"Staging local Service Definition: {sd_file}")
 arcpy.StageService_server(sddraft_file, sd_file)
 print(4*" " + f"Overwriting online service definition: {sd_item.title}")
 sd_item.update(data=sd_file)
 print(4*" " + f"Publishing feature service")
 pub_params = {"editorTrackingInfo": {"enableEditorTracking": 'true', "preserveEditUsersAndTimestamps": 'true'}}
 fs = sd_item.publish(publish_parameters=pub_params, overwrite=True)

You should be able to tweak this slightly to publish a new service, rather than overwrite an existing one.

Note that instead of leaving a fGDB sitting in AGOL, this will leave a service definition sitting in AGOL. Again, you can delete this (in code) if you wish. However, that would mean you could no longer overwrite the feature service in-place.

answered Jul 24 at 22:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.