Adding relationship to hosted feature service

1699
1
10-08-2018 11:18 AM
KenMorefield
Occasional Contributor

Is it possible to Add new Layers and Relationships to a Hosted Feature Service, without having to download all of the data to a File Geodatabase, building the layers and relationships in the File Geodatabase and then republishing to ArcGIS Online?  Is this possible by using the "Add to Definition" function in the REST API Admin?  It is problematic to download to a file geodatabase first because I have editor tracking enabled.  It seems like it always changes the creator, editor, creation date and edit date fields - and it also doesn't seem to handle bringing the relationships across when downloading and using that workflow.  Any way to add a new layer and relationship directly in ArcGIS Online?  Thanks!

0 Kudos
1 Reply
JarrettGreen
New Contributor III

Hi Ken,

You can use the REST API Administrative directory to add and delete relationship classes. When adding or deleting, make sure to add or delete the relationship to both the origin and destination layers. I would advise against modifying existing relationship classes. I have broken hosted feature services by deleting and re-creating them with a different cardinality, from 1:M to 1:1. I'm thinking it is because the records in my destination were not already a 1:1. Modifying your hosted feature service from the REST admin is risky, but very fast and efficient when it works as expected. I recommend to always test first!

I have not found a way to add a new layer to an existing hosted feature service. It is possible to preserve editor tracking if you need to overwrite a hosted feature service. I have done it with a few lines of python in ArcGIS Pro using the ArcGIS API for Python. After downloading a file geodatabase of your hosted feature service and symbolizing in ArcGIS Pro, follow the steps below. You'll want to modify the bolded parameters.

First, you need to create the .sd file in ArcGIS Pro.

aprx = arcpy.mp.ArcGISProject(r'location of your .aprx file')
m = aprx.listMaps('the name of your map in ArcGIS Pro')[0]
print('creating sd draft')
arcpy.mp.CreateWebLayerSDDraft(m, r'C:\Projects\myServiceName.sddraft', 'myServiceName')
print('staging service')
arcpy.StageService_server(r'C:\Projects\myServiceName.sddraft', r'C:\Projects\myServiceName.sd')
print('complete')

Second, you need to add the .sd item you created to ArcGIS Online.

Third, you need to publish the .sd item in ArcGIS Pro.

from arcgis.gis import GIS
gis = GIS('pro')
#Parameters for editor tracking
print('starting')
pub_params = {'editorTrackingInfo' : {'enableEditorTracking':True, 'preserveEditUsersAndTimestamps':True}}
print('finding item')
sd_item = gis.content.get('the item id of your .sd file in ArcGIS Online')
print('publishing')
sd_item.publish(publish_parameters = pub_params, overwrite = True)
print('complete')

Fourth, you need to modify and check the capabilities of your service. You will have to enable editing and other parameters as needed. It should be possible to control these in the pub_params parameter if you wanted to streamline it.

It is a bit cumbersome, but if your business requirements rely heavily on editor tracking, I have found this to be very useful!

Jarrett