Skip to content

Example of how to create joined view use the API for Python #2267

@astjohn-RM

Description

@astjohn-RM

Is your feature request related to a problem? Please describe.
I am trying to automate creation of hosted joined views, as we use many of them that must be maintained to standards across AGO organizations.
I am led to believe that this is the intended output of this method, but can not figure it out: https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.ViewManager.create_join_layer

For example, some of my latest attempt:

from arcgis.gis._impl._dataclasses._viewdc import JoinType
from arcgis.features import FeatureLayer, FeatureLayerCollection, Table

flc_item = gis.content.search(rf"id:<hfl_itemid>", item_type="Feature Service")[0]

for lyr in flc_item.layers:
    if (len(lyr.properties['relationships']) > 0):
       # Initialize feature layer collection from URL of specific layer
        flc = FeatureLayerCollection(flc_item.url + r"/" + str(lyr.properties.id))
        for rel in lyr.properties['relationships']:
            if ("WorkOrders" in rel['name']):
               # Initial table from URL of related table to feature layer (in same hosted feature service)
                tbl = Table(flc_item.url + r"/" + str(rel['relatedTableId']))
                view_item = flc.view_manager.create_join_layer(
                                join_name=rf"{rel['name']} test joined view", 
                                target_join_fields=["FeatureGUID"],
                                join=tbl,
                                join_fields=["GlobalID"],
                                join_type=JoinType.INNER,
                                include_geometry=True,
                                owner=gis.users.me
                            )
                print(rf"Created joined view for relationship: {rel['name']}")

Describe the solution you'd like
I would love to see a working example to create a hosted joined view between a feature layer and its related table, so I can better understand how the manager classes are used.
I've run into various errors as I am trying many things here, and am not sure whether I am running into bugs or misunderstandings.

Additional context
Also note that I must use private classes to access the JoinType enumeration.

Activity

astjohn-RM

astjohn-RM commented on May 29, 2025

@astjohn-RM
Author

Bump.

Note that there is also a bug here, since I am importing the private JoinType class.

nanaeaubry

nanaeaubry commented on Jun 13, 2025

@nanaeaubry
Contributor

@achapkowski Can you please advise?

astjohn-RM

astjohn-RM commented on Jun 16, 2025

@astjohn-RM
Author

@achapkowski Update on my status and some additional observations:

  • I am able to create some joined views (code snippet below)
for viewset in all_views:
  for view in viewset:
      flc_item = gis.content.search(rf"id:{view['hfl_itemid']}", item_type="Feature Service")[0]
      #print(flc_item)
      if not flc_item:
          print(rf"HFL {view['hfl_itemid']} not found.")
      else:
          tbl = Table(flc_item.url + r'/' + str(view['table_id']))
          #print(tbl)
          view_exists = check_view_existence(gis, view['name'].replace(" ", "_") + "_JOINEDTEST")
          if not view_exists:
              view_item = flc_item.view_manager.create_join_layer(
                  join_name=view['name'].replace(" ", "_") + "_JOINEDTEST", 
                  target_join_fields=[view['join_field']],
                  join=tbl,
                  join_fields=[view['target_field']],
                  join_type=JoinType.INNER,
                  include_geometry=True,
                  owner=gis.users.me
              )
              print(rf"Created {view['name']}")
          else:
              print(rf"Joined view {view['name']} already exists.")

This only works for point feature layers.

  • Line and polygon feature layers that I attempt to join to their associated work order table tbl = Table(flc_item.url + r'/' + str(view['table_id'])) (in the same Hosted Feature Service) do not have any data in the join.

The point feature layer-table joins do not include the feature layer fields, only the table's fields.

There is no option for cardinality. Is this assuming/supporting 1:M only?

  • If so, that is fine with me, all of my joined views are one-to-many: looking or clarification.

The documentation of join_fields versus target_join_fields is unclear to me.

  • My code snippet is partially working but it seems backwards: my view['join_field'] is in the "source feature layer" and my view['target_field'] is a field in the "target table layer"

Are tables meant to be supported as a target?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nanaeaubry@astjohn-RM

        Issue actions

          Example of how to create joined view use the API for Python · Issue #2267 · Esri/arcgis-python-api