I'm currently trying to create a script which will do a few things, including loading a webmap, filtering it based on a definition query, zooming to the resulting shape extent, and then creating a PDF using a custom layout. I've managed to do all this using a local project in ArcGIS Pro, but had trouble exporting it (mainly because I was using the "CURRENT" project, and I'm now trying to do this via the Portal).
In any case, I'm having to redo a fair bit of code. I'm getting stuck where I refer to the layer so I can perform a definition query on it. Basically, I'd ideally like the lyr object to refer to a layer, so I can do a "lyr.definitionQuery = etc"
This is what I've got so far. I know the syntax is wrong, I'm just looking for the right one:
import arcpy
from arcgis.mapping import WebMap
from arcgis.gis import GIS
#get variables
RequestedId = arcpy.GetParameterAsText(0)
cUN = arcpy.GetParameterAsText(1)
cPW = arcpy.GetParameterAsText(2)
#connect
gis = GIS(r"https://mydomain.com/portal", cUN, cPW)
#try and load the webmap
wm_item = gis.content.get("webmap_id_here")
wm = WebMap(wm_item)
#try to access the layer
arcpy.AddMessage(wm.layers)
lyr = wm.layers("ThisIsMyLayersTitle")[0]
For reference, and probably to see more what I'm trying to do, this is my full (old) code that works on a local project:
import arcpy
RequestedId = arcpy.GetParameterAsText(0)
#select our current map, and then the layer we want, and grab our Id from parameters
aprx = arcpy.mp.ArcGISProject("CURRENT")
RequestedForestId = arcpy.GetParameterAsText(0)
m = aprx.listMaps("MyWebMap")[0]
lyr = m.listLayers("MyLayer")[0]
#set our definition query
lyr.definitionQuery = "Id = '" + RequestedId + "'"
#define our extents
extents = []
#loop through and add our shape extent
with arcpy.da.SearchCursor(lyr,'SHAPE@') as cursor:
for row in cursor:
if row[0]:
extents.append(row[0].extent)
#get our actual extent
eXMin = min([ext.XMin for ext in extents])
eXMax = max([ext.XMax for ext in extents])
eYMin = min([ext.YMin for ext in extents])
eYMax = max([ext.YMax for ext in extents])
#now go into the layout
lyt = aprx.listLayouts("Layout")[0]
mf = lyt.listElements("mapframe_element", "WEBMAP_MAP_FRAME")[0]
mf.map = m
myExtent = arcpy.Extent(eXMin, eYMax, eXMax, eYMin, 0, 0)
mf.camera.setExtent(myExtent)
mf.camera.scale = mf.camera.scale * 1.1
m.defaultCamera = mf.camera
lyt.exportToPDF("C:\\temp\\mylayout.pdf")