Retrieving all feature layer views with View Manager list()

1074
6
10-24-2023 11:03 AM
Labels (1)
GIS_utahDEM
Occasional Contributor II

I am trying to return a list of all feature layer views of a specific item. I am able to successfully do this with the arcgis.gis ViewManager .list() method, however it only returns the first 100 (I know, I've got a lot of view layers). Is there a way to overrule this and make it return all of them? I should have about 350 for every jurisdiction in my state. 

0 Kudos
6 Replies
MobiusSnake
MVP Regular Contributor

I can't say that's a limitation I've run into, but if you connect to the Views endpoint of the REST API, can you see all 350 or so there?  The URL would be something like this:

https://services350.arcgis.com/somerandomchars/ArcGIS/rest/services/YourRootService/FeatureServer/views?f=json 

Probably need a token in there too...

Anyhow, does that endpoint only show 100 views?  If so, it's probably a limitation of the REST API rather than the Python API.

0 Kudos
GIS_utahDEM
Occasional Contributor II

I'm not sure I follow how to get to that link (it asks for a token but I'm not sure how to generate one?), but if I view the REST Services Directory for the Views from the feature layer I'm able to see all of them...does that help?

0 Kudos
MobiusSnake
MVP Regular Contributor

Yeah, that does seem to indicate it's a limitation of the API then.

If you can't find a workaround using the Python API, you could connect to that page/endpoint using the requests module, urllib, etc. and get the full list that way.

0 Kudos
GIS_utahDEM
Occasional Contributor II

Thanks for the info -- do you have any resources/references on those that you could point me towards? haha

0 Kudos
MobiusSnake
MVP Regular Contributor

I don't know of any Esri resources that show how to use the REST API with Python, but the full REST API documentation is here (it's language-agnostic, however):

https://developers.arcgis.com/rest/

Here's some doc on using the requests module, it's really handy if you're not familiar with it, I use it all the time to access non-GIS data:

https://requests.readthedocs.io/en/latest/

0 Kudos
EarlMedina
Esri Regular Contributor

It looks like the problem is the logic as written doesn't incorporate paging. ViewManager.list() uses the relatedItems endpoint which itself does support paging. The supporting method that needs to be updated is related_items in arcgis/gis/__init__.py.

You could fix it rather easily by introducing a while loop and adding "start" and "num" parameters in the postdata. The num value should be 100 based on what you've shared. If you decide to give this a try, I would recommend experimenting in a new python environment as changes to the source voids technical support.

I've not tested this, but something like this should get you more or less started (you will need to open/save the file as an Administrator):

 

    def related_items(self, rel_type: str, direction: str = "forward"):
        if rel_type not in self._RELATIONSHIP_TYPES:
            raise Error("Unsupported relationship type: " + rel_type)
        if not direction in self._RELATIONSHIP_DIRECTIONS:
            raise Error("Unsupported direction: " + direction)

        related_items = []

        postdata = {"f": "json"}
        postdata["relationshipType"] = rel_type
        postdata["direction"] = direction
        postdata["num"] = 100
        postdata["start"] = 0
        keep_going = True
        while keep_going:
            resp = self._portal.con.post(
                "content/items/" + self.itemid + "/relatedItems", postdata
            )
            if len(resp["relatedItems"]) > 0:
                postdata["start"] = postdata["start"] + 100
                for related_item in resp["relatedItems"]:
                    related_items.append(Item(self._gis, related_item["id"], related_item))
            else:
                keep_going = False
        return related_items

 

 

 

 

 

In any case, I would recommend logging an enhancement with Esri Technical Support for paging to be added.

 

 

 

0 Kudos