Skip to content

IndexError and corrupted hosted feature service when using the ViewManager create method #2537

Open

Description

@astjohn-RM

Describe the bug
The create method in the ViewManager class throws an IndexError when using the query or visible_fields parameters, for hosted feature service containing more than one feature layer.

Removing the query and visible_fields parameters also throws the IndexError after successfully creating just one view from the parent hosted feature service.
It seems to corrupts the parent hosted feature service definition in some way(s), where the layers are no longer recognized via the sharing REST endpoint and in the view creation/update UI.
See screenshots for examples of expected versus corrupted.

To Reproduce
Steps to reproduce the behavior:

# example parameters
view = {
      "name": "Sanitary Lift Stations view",
      "hfl_itemid": sanitary_hfl_itemid,
      "layer_id": 0,
      "vis_field": ["*"],
      "filter": "Status <> 'Abandoned' AND StructureType = 'Lift Station'"
  }

view_item = flyr_item.view_manager.create(
                        name=new_view_name, 
                        view_layers=[flyr_item.layers[view['layer_id']]], 
                        allow_schema_changes=False, 
                        updateable=False, 
                        capabilities="Query", 
                        preserve_layer_ids=True, 
                        query=view['filter'], 
                        visible_fields=["OBJECTID", "GlobalID"] + view['vis_field']
                    )

error:

Traceback (most recent call last):
  File "C:\Users\<path to .py>", line 140, in <module>
    main()
    ~~~~^^
  File "C:\Users\<path to .py>", line 75, in main
    view_item = flyr_item.view_manager.create(
            name=view_name,
    ...<5 lines>...
            query="Status <> 'Abandoned' AND StructureType = 'Lift Station'" if view_name == "Sanitary_Lift_Stations_view" else "Status <> 'Abandoned' AND (StructureType = 'Outfall' OR StructureType = 'Grass Swale Outfall')"
        )
  File "C:\Users\AStJohn\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\arcgis\gis\__init__.py", line 20804, in create
    return mgr.create_view(
           ~~~~~~~~~~~~~~~^
        name=name,
        ^^^^^^^^^^
    ...<14 lines>...
        query=query,
        ^^^^^^^^^^^^
    )
    ^
  File "C:\Users\AStJohn\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\arcgis\features\managers.py", line 3110, in create_view
    set_visible_fields_and_query(item, visible_fields, query, gis)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AStJohn\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\arcgis\features\managers.py", line 3078, in set_visible_fields_and_query
    fields = item.layers[0].properties["fields"]
             ~~~~~~~~~~~^^^
IndexError: list index out of range

Screenshots
After running the view create() method, with OR without the query and visible_fields parameters, the UI no longer recognizes feature layers and only tables are available when creating or updating the view.

Image Image

Expected behavior
This API call has been working for over a year in our scripts, creating hundreds of views programmatically. I expect that the views be created with query and visible_fields parameters defined and that the parent hosted feature service is not altered in any detrimental way.

Platform (please complete the following information):

  • OS = Windows 11
  • Browser = Chrome
  • Python API Version = 2.4.2

Additional context
Add any other context about the problem here, attachments etc.

Activity

  1. kyroskoh commented on Aug 13, 2026

    @kyroskoh

    Root cause (confirmed in arcgis 2.4.2 and 2.4.3)

    This public repo is docs/samples only, so a library source PR cannot land here. The bug is in the closed-source package: FeatureLayerCollectionManager.create_view (arcgis/features/managers.py), which ViewManager.create calls.

    1. Parent hosted feature service corruption

    Near the end of create_view, after correctly resolving the new view item:

    view_item = content.get(res["itemId"])  # correct — the new view
    # ...
    view_item = item  # BUG — `item` is the PARENT serviceItemId
    update_item_data(view_item, item, view_layers)

    update_item_data then writes a filtered data.layers subset onto the parent. On multi-layer parents with view_layers=[one layer], the parent item JSON is overwritten so Portal/AGOL UI stops treating feature layers as layers (matches the screenshots).

    Suggested fix: delete the reassignment and keep the original view item:

    # view_item = item  # remove this line
    update_item_data(view_item, item, view_layers)

    2. IndexError on item.layers[0]

    fs_view.manager.add_to_definition(add_def, future=gis._is_arcgisonline)  # not awaited on AGOL
    # ...
    fields = item.layers[0].properties["fields"]  # IndexError when layers still empty

    On AGOL, future=True returns a Future that is never .result()'d before set_visible_fields_and_query runs. Also, query/visible_fields are applied only to layers[0], not every view layer.

    Suggested fix:

    add_result = fs_view.manager.add_to_definition(add_def, future=gis._is_arcgisonline)
    if hasattr(add_result, "result"):
        add_result.result()
    fs_view._hydrated = False
    fs_view.refresh()

    And in set_visible_fields_and_query, guard empty layers and loop all flc.layers (treat visible_fields containing "*" as all fields visible).

    Temporary workaround for 2.4.2 / 2.4.3 users

    Full usage, limits, removal steps, and tests: misc/tools/issue_2537/README.md on branch fix/issue-2537-viewmanager-create.

    Monkey-patch module: fix_viewmanager_create_2537.py

    from misc.tools.issue_2537 import apply
    apply()
    # then use view_manager.create(...) as usual

    Or copy the file next to your script:

    import fix_viewmanager_create_2537 as fix
    fix.apply()

    It restores parent item JSON after create, awaits AGOL add_to_definition, writes layer JSON onto the view, and applies query/visible_fields to each view layer.

    Mock / smoke tests

    No live org required:

    python -m unittest misc.tools.issue_2537.test_fix_viewmanager_create_2537 -v

    See test_fix_viewmanager_create_2537.py.

    Removing the patch when Esri ships a fix

    See When Esri releases a fix in the README. Short version:

    1. Upgrade to the arcgis release that includes the #2537 fix (check the release notes).
    2. Delete or stop calling the workaround imports, for example remove:
    from misc.tools.issue_2537 import apply
    apply()

    or:

    import fix_viewmanager_create_2537 as fix
    fix.apply()
    1. If a long-running process still has the monkey-patch loaded, restore the original method before restarting on the fixed package:
    from misc.tools.issue_2537 import unapply
    unapply()
    1. Re-run a multi-layer view_manager.create(..., query=..., visible_fields=...) smoke test and confirm the parent service data.layers is unchanged.
    2. Optionally delete the local misc/tools/issue_2537/ copy so the hotfix cannot be reapplied by mistake.
  2. added a commit that references this issue on Aug 13, 2026
  3. nanaeaubry commented on Aug 13, 2026

    @nanaeaubry
    Contributor

    @kyroskoh @astjohn-RM Thank you for reporting this and thoroughly explaining the issue.

    We are going to take a look at fixing.

    Update: We found this issue but it is set to be fixed in 2.4.4 (Released in Fall). I will leave this issue open, after release please test and we can close if correctly addressed.

  4. added 2 commits that reference this issue on Aug 13, 2026
  5. nanaeaubry commented on Sep 10, 2026

    @nanaeaubry
    Contributor

    @kyroskoh
    A pre-release has been made with changes that fix this issue:

    conda install -c esri/label/prerelease -c esri arcgis=2.4.4 arcgis-mapping=5.1.0 arcgis-geometry=1.1.0

  6. nanaeaubry commented on Oct 9, 2026

    @nanaeaubry
    Contributor

    @kyroskoh

    We have released to conda, please test this and ensure it is working as expected. A pypi release will be made in the coming weeks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions