<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Joined View Layer in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1407677#M9856</link>
    <description>&lt;P&gt;Thank you for the detailed answer. I will give this a try!&lt;/P&gt;</description>
    <pubDate>Tue, 09 Apr 2024 18:57:14 GMT</pubDate>
    <dc:creator>gargarcia</dc:creator>
    <dc:date>2024-04-09T18:57:14Z</dc:date>
    <item>
      <title>Joined View Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1406297#M9844</link>
      <description>&lt;P&gt;Is it possible to use the python Api to create a joined view layer? I have checked the reference guide and saw the join analysis tool, but it didn't appear to have a parameter for output type like using the tool in the classic map viewer has. I also see the function for creating a view, but nothing associated about joins. If it is not possible with the python Api are there alternative methods to programmatically create joined view layers?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="gargarcia_0-1712319926501.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/100189iCC1CC84A065E4ED5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="gargarcia_0-1712319926501.png" alt="gargarcia_0-1712319926501.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Apr 2024 12:28:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1406297#M9844</guid>
      <dc:creator>gargarcia</dc:creator>
      <dc:date>2024-04-05T12:28:33Z</dc:date>
    </item>
    <item>
      <title>Re: Joined View Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1406655#M9845</link>
      <description>&lt;P&gt;You can accomplish this, but it requires a bit of setup and understanding of how things work. Let me give you an example.&lt;/P&gt;&lt;P&gt;Suppose I want to join &lt;STRONG&gt;Feature Layer A&lt;/STRONG&gt; to &lt;STRONG&gt;Table B.&amp;nbsp;&lt;/STRONG&gt;I know the field I want to join on is named "Activity," and I want to perform a Left Join.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Begin with necessary imports, initial setup:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayer, Table, FeatureLayerCollection

gis = GIS("https://www.arcgis.com", username="hari", password="seldon")
fl_url = "https://services3.arcgis.com/xxxxxxxxxxx/arcgis/rest/services/pntLayer/FeatureServer/0"
tbl_url = "https://services3.arcgis.com/xxxxxxxxxxx/arcgis/rest/services/TableToJoin/FeatureServer/0"

fl = FeatureLayer(fl_url, gis)
tbl = Table(tbl_url, gis)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next, you'll want to add an index on the join field to both services:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;index_to_add = {"indexes":[
    {
        "name": "Activity_Index", 
        "fields": "Activity", 
        "isUnique": False, 
        "isAscending": True, 
        "description": "Activity_Index"
    }
]}

fl.manager.add_to_definition(index_to_add)
tbl.manager.add_to_definition(index_to_add)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Create a blank view service, initialize a FeatureLayerCollection from the result for later use:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;view_service = gis.content.create_service(name="joined_view", is_view=True)
view_flc = FeatureLayerCollection.fromitem(view_service )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, you need to think about which fields you want from the source feature layer and source table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sourceFeatureLayerFields = [
    {
        "name": "Activity",
        "alias": "Activity",
        "source": "Activity"
    },
    {
        "name": "Description",
        "alias": "Description",
        "source": "Description"
    },
    {
        "name": "StartDate",
        "alias": "StartDate",
        "source": "StartDate"
    },
    {
        "name": "EndDate",
        "alias": "EndDate",
        "source": "EndDate"
    }
]

sourceTableFields = [
    {
        "name": "Note",
        "alias": "Note",
        "source": "Note"
    }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is where the magic happens. We use some of the info above to create a definition we can use to end up with our desired joined view:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;field_to_join_on = "Activity"
view_lyr_name = "sampleJoinedView"
definition_to_add = {
  "layers": [
    {
      "name": view_lyr_name,
      "displayField": "",
      "description": "AttributeJoin",
      "adminLayerInfo": {
        "viewLayerDefinition": {
          "table": {
            "name": "sampleJoinedView",
            "sourceServiceName": fl.properties.name,
            "sourceLayerId": 0,
            "sourceLayerFields": sourceFeatureLayerFields,
            "relatedTables": [
              {
                "name": "testjoin",
                "sourceServiceName": tbl.properties.name,
                "sourceLayerId": 0,
                "sourceLayerFields": sourceTableFields,
                "type": "LEFT",
                "parentKeyFields": [
                  field_to_join_on
                ],
                "keyFields": [
                  field_to_join_on
                ],
                "topFilter": {
                  "groupByFields": field_to_join_on,
                  "orderByFields": "OBJECTID ASC",
                  "topCount": 1
                }
              }
            ],
            "materialized": False
          }
        },
        "geometryField": {
          "name": f"{view_lyr_name}.Shape"
        }
      }
    }
  ]
}

view_flc.manager.add_to_definition(definition_to_add)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Put it all together and we have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayer, Table, FeatureLayerCollection

gis = GIS("https://www.arcgis.com", username="hari", password="seldon")
fl_url = "https://services3.arcgis.com/xxxxxxxxxxx/arcgis/rest/services/pntLayer/FeatureServer/0"
tbl_url = "https://services3.arcgis.com/xxxxxxxxxxx/arcgis/rest/services/TableToJoin/FeatureServer/0"

fl = FeatureLayer(fl_url, gis)
tbl = Table(tbl_url, gis)

index_to_add = {"indexes":[
    {
        "name": "Activity_Index", 
        "fields": "Activity", 
        "isUnique": False, 
        "isAscending": True, 
        "description": "Activity_Index"
    }
]}

fl.manager.add_to_definition(index_to_add)
tbl.manager.add_to_definition(index_to_add)

view_service = gis.content.create_service(name="joined_view", is_view=True)
view_flc = FeatureLayerCollection.fromitem(view_service )

sourceFeatureLayerFields = [
    {
        "name": "Activity",
        "alias": "Activity",
        "source": "Activity"
    },
    {
        "name": "Description",
        "alias": "Description",
        "source": "Description"
    },
    {
        "name": "StartDate",
        "alias": "StartDate",
        "source": "StartDate"
    },
    {
        "name": "EndDate",
        "alias": "EndDate",
        "source": "EndDate"
    }
]

sourceTableFields = [
    {
        "name": "Note",
        "alias": "Note",
        "source": "Note"
    }
]

field_to_join_on = "Activity"
view_lyr_name = "sampleJoinedView"
definition_to_add = {
  "layers": [
    {
      "name": view_lyr_name,
      "displayField": "",
      "description": "AttributeJoin",
      "adminLayerInfo": {
        "viewLayerDefinition": {
          "table": {
            "name": "sampleJoinedView",
            "sourceServiceName": fl.properties.name,
            "sourceLayerId": 0,
            "sourceLayerFields": sourceFeatureLayerFields,
            "relatedTables": [
              {
                "name": "testjoin",
                "sourceServiceName": tbl.properties.name,
                "sourceLayerId": 0,
                "sourceLayerFields": sourceTableFields,
                "type": "LEFT",
                "parentKeyFields": [
                  field_to_join_on
                ],
                "keyFields": [
                  field_to_join_on
                ],
                "topFilter": {
                  "groupByFields": field_to_join_on,
                  "orderByFields": "OBJECTID ASC",
                  "topCount": 1
                }
              }
            ],
            "materialized": False
          }
        },
        "geometryField": {
          "name": f"{view_lyr_name}.Shape"
        }
      }
    }
  ]
}

view_flc.manager.add_to_definition(definition_to_add)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are other options you can play around with in the view layer definition, but this should give you a good starting point. T&lt;SPAN&gt;o get a better idea of what your definition needs to look like, y&lt;/SPAN&gt;ou could use the UI to create the exact view you want and capture the network traffic right after you click the button to create the joined view. The call you need to pay most attention to is "addToDefinition".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps you and a lot of other people.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Apr 2024 20:47:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1406655#M9845</guid>
      <dc:creator>EarlMedina</dc:creator>
      <dc:date>2024-04-05T20:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Joined View Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1407677#M9856</link>
      <description>&lt;P&gt;Thank you for the detailed answer. I will give this a try!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 18:57:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1407677#M9856</guid>
      <dc:creator>gargarcia</dc:creator>
      <dc:date>2024-04-09T18:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: Joined View Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1569036#M10972</link>
      <description>&lt;P&gt;Thanks for a detailed post!&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;For anybody wondering, if you want to create a table instead of a feature layer, just remove the "geometryField" key in the "adminLayerInfo" dict.&lt;/P&gt;&lt;P&gt;I played around trying to change "table" to "layer" or "feature layer" in the "viewLayerDefinition" dict also, but that just results in an error.&lt;/P&gt;&lt;P&gt;You can also reuse all the fields in your sources.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fl_fields = []
tbl_fields = []

for f in fl.properties.fields:
    fl_fields.append(dict(f))
for f in tbl.properties.fields:
    tbl_fields.append(dict(f))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;and then put fl_fields in "sourceLayerFields" for your "table" and tbl_fields in "sourceLayerFields" for your "relatedTables".&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2024 19:48:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/joined-view-layer/m-p/1569036#M10972</guid>
      <dc:creator>mikaël</dc:creator>
      <dc:date>2024-12-16T19:48:38Z</dc:date>
    </item>
  </channel>
</rss>

