Adding related table to ArcGIS Online feature layer

57761
73
Jump to solution
01-08-2019 10:26 AM
EricGardecki1
New Contributor III

I'm trying to figure out how to add a relationship class (related table one-to-many) to a feature layer within AGOL. I do not have ArcGIS Desktop or Server (do have QGIS but I don't think that will help here).

The only info I was able to find led me to believe that it may be possible by editing the service definition JSON but I can't find any reference as to how to edit it to make it work.

What are my options here?

Also, is viewing/editing/adding to related tables supported in AGOL web apps yet (not just Collector)?

Thanks!!

1 Solution

Accepted Solutions
KhaledHassen
Esri Contributor

Eric,

Sorry this is not documented well so I could not find the doc for you to use. Will work with the team to get this documented.

But you will need to follows the example I have sent you. Only what I have included in my example is needed. Before you do anything, pl. send me the full url (strip the token) and the json used. The json you have used in your last message is not correct. 

>Also, how am I defining what the 2nd layer is?  As in, a pointer to the name of the 2nd layer (table) so it knows which >table is related?  I do not see any such reference in your sample.

 All what you really need to figure out is the followings:

- Origin layer Id

- Destination layer id

- Primary key in the origin layer (field name that acts as a primary key)

- Foreign key in the destination layer. 

You just need to replace these in the example json I sent you.

I have added some comments below.

https://url/ArcGIS/rest/admin/services/<serviceName>/FeatureServer/addToDefinition

{

  "layers" : [

  {

    "id" : 0,

    "relationships" : [

    {

      "id" : 1,

      "name" : "myRel",  // <name> for your relationshp. Any name is accepted

      "relatedTableId" : 1,  //  (table or related layer Id)  

      "cardinality" : "esriRelCardinalityOneToMany",

      "role" : "esriRelRoleOrigin",

      "keyField" : "<PrimaryKey>",  // Origin primary key like GlobalId if the relationship based on globalId

      "composite" : <true | false> // Not sure if you need composite but set it to false if you do not need it

    }

   ]

  },

  {

    "id" : 1,

    "relationships" : [

    {

      "id" : 1,

      "name" : "myRel",  // use the same name you defined above for the relationship name.

      "relatedTableId" : 0,  // This is the layerId of the origin

      "cardinality" : "esriRelCardinalityOneToMany",

      "role" : "esriRelRoleDestination",

      "keyField" : "<ForignKey>",

      "composite" : <true | false> // // Not sure if you need composite but set it to false if you do not need it

    }

   ]

 }

 ]

}

View solution in original post

73 Replies
AdrianWelsh
MVP Honored Contributor

Eric,

Take a look at this thread:

Add data to a related table in ArcGIS Online 

It looks like this idea is still in the ArcGIS Ideas‌ phase but there is a potential work around in that thread that could help your situation.

Also, this FAQ mentions that AGOL does support joins and relates and lists a couple of articles on how to do this. It's not very robust though:

FAQ: Does ArcGIS Online support joins and relates? 

One more thread: this walks through how to properly share the related table in Pro:

https://community.esri.com/thread/198458-publish-hosted-feature-service-with-related-tables-wont-wor... 

...but not having desktop might pose as a problem.

EricGardecki1
New Contributor III

Thanks Adrian.

Based on that info it does appear that AGOL does at least support the usage of the relates.  I still need to figure out how to actually create one in AGOL without Desktop.

Thanks

AdrianWelsh
MVP Honored Contributor

I'm not sure how QGIS handles geodatabases but you can add a table to an AGOL map from a zipped geodatabase. Take a look at this article:

FAQ: Is it possible to publish standalone tables to ArcGIS Online? 

Instructions:

Alternatively, the standalone table can also be opened in ArcGIS Online without using the publishing method. To do so:

  1. Close all ArcGIS programs and zip the geodatabase that contains the table.
  2. Upload the zipped geodatabase file to ArcGIS Online as an item in My Content.

Now relating it is a different story. I would have to play around with some stuff to see if it worked for me, so maybe it's worth trying that method?

EricGardecki1
New Contributor III

The problem with that is that it won't maintain the relationship as that info is stored in the mxd, not the gdb.  There is no way thru the interface to add a relationship in AGOL.  However, based on Khaled's reply below, there does seem to be a way!

KhaledHassen
Esri Contributor

We do support add relationship to existing feature service in online using feature service addToDefinition API.

Ex:

https://url/ArcGIS/rest/admin/services/<serviceName>/FeatureServer/0/addToDefinition

The origin layer and its related layer (table) has to exist. This API can only allow you to create a relationship between the two layers and be able to use queryRelated API on the layer.

In the Example below, you will need to define the relationship for each layer which reference that other layer.

Pl. note, the relationshipId for both layer is the same.

FYI we also support creating a view that joins both layers. Pl. let us know if you your workflow needs a view that has one layer that join both origin and destination layer.

Hope that helps.

Khaled

{

  "layers" : [

  {

    "id" : 0,

    "relationships" : [

    {

      "id" : 1,

      "name" : "myRel",

      "relatedTableId" : 1,

      "cardinality" : "esriRelCardinalityOneToMany",

      "role" : "esriRelRoleOrigin",

      "keyField" : "<PrimaryKey>",

      "composite" : <true | false>

    }

   ]

  },

  {

    "id" : 1,

    "relationships" : [

    {

      "id" : 1,

      "name" : "myRel",

      "relatedTableId" : 0,

      "cardinality" : "esriRelCardinalityOneToMany",

      "role" : "esriRelRoleDestination",

      "keyField" : "<ForignKey>",

      "composite" : <true | false>

    }

   ]

 }

 ]

}

EricGardecki1
New Contributor III

Great!  This is what I was expecting was necessary.  I will definitely play around with that.  Do you have a link to a reference that describes everything necessary in the "relationships" item and what the options are?

Thanks!

0 Kudos
EricGardecki1
New Contributor III

Khaled,

Am I adding the "layers" tag?  It isn't a part of the JSON of my test layer.  I think i'm misunderstanding something as how can there be 2 layers within 1 feature layer? 

The JSON I am looking at begins like this:

 {
"adminLayerInfo" : {
"tableName" : "...",
"geometryField" : {"name" : "Shape"},
"xssTrustedFields" : ""
},
"id" : 0,
"name" : "Point layer",
"type" : "Feature Layer", 

0 Kudos
KhaledHassen
Esri Contributor

Eric,

This is a feature service addToDefinition admin API and this is why you can and need to add the layers array.

Pl. use:

https://url/ArcGIS/rest/admin/services/<serviceName>/FeatureServer/addToDefinition

and pass similar json to the one I have added in my response and let me know if it does not work for you.

Pl. note it is /FeatureServer/addToDefinition so it is not a layer admin call.

Thanks

Khaled

0 Kudos
KhaledHassen
Esri Contributor

Sorry I have a typo in the url for addToDefinition. It should be the feature service (not the layer) addToDefinition

https://url/ArcGIS/rest/admin/services/<serviceName>/FeatureServer/addToDefinition

Khaled Hassen