Environment: Running ArcGIS API for Python version 2.0.1 from Pro 3.0.2.
I am trying to use the ArcGIS API for Python to create a view of an AGOL hosted feature layer and set a definition query. This guide seems to be the most definitive documentation of the process, but it's not working for me. Here is the code I have that follows this process:
from arcgis import GIS
from arcgis.features import FeatureLayerCollection
source_item_id = '<your item id>'
view_def = {
'viewDefinitionQuery': "State_FIPS IN ('09','23','25','33','44','50')"
}
view_name = 'TEST_create_view_python_01'
gis = GIS(profile='AGOL')
source_item = gis.content.get(source_item_id)
source_flc = FeatureLayerCollection.fromitem(source_item)
new_view = source_flc.manager.create_view(name=view_name)
view_search = gis.content.search(view_name)[0]
view_flc = FeatureLayerCollection.fromitem(view_search)
service_layer = view_flc.layers[0]
service_layer.manager.update_definition(view_def)
I copied the definition query from a view I manually created in a browser, so I'm pretty sure that's correct. It runs without errors or warnings. The new view shows up as an AGOL item, but when I add the view to a webmap, I see this error:
And adding it to a map in Pro gives me this:
Next, I tried one little tweak. After defining the source_flc variable, I create a list of layers and pass that to the create_view function's view_layers parameter. Otherwise, the code is the same:
view_layers = [source_flc.layers[0]]
new_view = source_flc.manager.create_view(name=view_name, view_layers=view_layers)
I can add it to Pro and it renders fine, but the popups are just a regular attribute field display, not my custom popup.I went to edit the popups in my layer and found that the Visualization tab (where you set the popup) is missing!
When I click "Open in Map Viewer", nothing loads.
If I use the "Add" button, the view shows up. If I click the "+Add" button, it changes to an "X Remove" button, but nothing shows up in the layer list or renders on the map.
I can manually create a working view in a web browser. I am getting errors when I try this with other source layers. So this doesn't seem like a data issue. What am I doing wrong?
That worked! All I did was add that one line, how odd. Thanks!
I always encounter problems when using create_view() so instead a create an empty service stating it's a view and then add layers to it.
# region create empty service
license = ""
me = gis.users.me.username
email = gis.users.me.email
serviceTitle = "" # title of service
serviceName = serviceTitle.replace(" ", "_") # I change spaces with underscores for the name of the service
serviceDescription = "" # description of service
folder = "" # agol folder
groupName = "" # group name if you want to share it to a group
tags = "" # tags separated by commas if need be
ip = {
"type": "Feature Service",
"typeKeywords": "",
"description": serviceDescription,
"title": serviceTitle,
"tags": tags,
"snippet": serviceDescription,
"accessInformation": email,
"licenseInfo": license,
"access": "private",
"commentEnabled": False,
"culture": "fr-ca"
}
viewService = gis.content.create_service(
#item_id="", # you can state the item_id if you want to keep the same as a previous service
name=serviceName,
service_description=serviceDescription,
capabilities="Query,Create",#,Editing,Sync,ChangeTracking",
description=serviceDescription,
copyright_text=license,
wkid=3857,
service_type="featureService",
owner=me,
folder=folder,
item_properties=ip,
is_view=True,
tags=tags,
snippet=serviceDescription
)
resultProtect = viewService.protect(enable=True)
resultAuth = viewService.content_status = "authoritative"
queryStrGroup = "title: "+groupName
queryStrGroup += " owner: "+me
autoGroup = [group for group in gis.groups.search(query=queryStrGroup, max_groups=5) if group.title == groupName][0]
addToGroup = viewService.sharing.groups.add(autoGroup)
viewFlc = FeatureLayerCollection.fromitem(viewService)
# resultUp = viewFlc.manager.update_definition({"isUpdatableView": True}) # not sure yet what isUpdatableView does ?
# endregion
# region view definition
# get your source service where queryStr si a string to search and sourceTitle is the exact name of your source service
serviceSourceItem = [item for item in gis.content.search(query=queryStr, item_type="Feature Layer Collection", max_items=10) if item.title == sourceTitle][0]
srcFlc = FeatureLayerCollection.fromitem(serviceSourceItem)
name = ""
id = 0
agolIdTarget = getCoucheTableId(serviceSourceItam, name)[2] #i have a function to get the layer id from the source service
ad = None
ad = {
"layers": [
{
"adminLayerInfo": {
"viewLayerDefinition": {
"sourceServiceName": srcFlc.manager.properties.adminServiceInfo.name,
"sourceLayerId": agolIdTarget, # id of the source layer/table
"sourceLayerFields": "*"
}
},
"id": id,
"name": name,
"description": serviceDescription
}
]
}
resultAd = viewFlc.manager.add_to_definition(ad)
# endregion
Hope that helps!