Trying to add a layer (Portal item) to a webmap. The ESRI example for this I'm looking at is very simple:
streets_item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"LA Streets"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Map Service"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
wm <SPAN class="operator token">=</SPAN> WebMap<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># create an empty web map with a default basemap</SPAN>
wm<SPAN class="punctuation token">.</SPAN>add_layer<SPAN class="punctuation token">(</SPAN>streets_item<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Okay, so I have a web map that's not empty that I created like this:
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>type<SPAN class="punctuation token">(</SPAN>target_item<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>mapping <SPAN class="keyword token">import</SPAN> WebMap
web_map_obj <SPAN class="operator token">=</SPAN> WebMap<SPAN class="punctuation token">(</SPAN>target_item<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>type<SPAN class="punctuation token">(</SPAN>web_map_obj<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#>>> <class 'arcgis.gis.Item'></SPAN>
<SPAN class="comment token">#>>> <class 'arcgis.mapping._types.WebMap'></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The print() statements are only there to assure myself that I'm actually looking at items on and not itemId's or who knows what. So I have a legit Python web map based on on a web map item in Portal. As a check, I do...
web_map_obj<SPAN class="punctuation token">.</SPAN>layers
<SPAN class="comment token"># prints out all the layers (map image layers) that nelong to the Portal web map</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
So now, I want to add another layer
newLayer <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>query <SPAN class="operator token">=</SPAN> <SPAN class="string token">"new layer"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># again just checking this is what I expect</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>type<SPAN class="punctuation token">(</SPAN>newLayer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
web_map_obj<SPAN class="punctuation token">.</SPAN>add_layer<SPAN class="punctuation token">(</SPAN>newLayer<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Well, this bombs...
<class 'arcgis.gis.Item'>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-191-62f982ec1c55> in <module>
13 # again just checking this is what I expect
14 print(type(newLayer))
---> 15 web_map_obj.add_layer(newLayer)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\mapping\_types.py in add_layer(self, layer, options)
323
324 for lyr in layer.layers: # recurse - works for all.
--> 325 self.add_layer(lyr, options)
326 return True # end add_layer execution after iterating through each layer.
327 else:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\mapping\_types.py in add_layer(self, layer, options)
510 fields_list = layer.properties.layerDefinition.fields
511
--> 512 for f in fields_list:
513 if isinstance(f, dict) or isinstance(f, PropertyMap):
514 field_dict = {'fieldName': f['name'],
TypeError: 'NoneType' object is not iterable
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>What's wrong with what I'm doing? I realize there is also arcgis.gis.<A href="https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Layer" rel="nofollow noopener noreferrer" target="_blank">Layer</A>. <SPAN style="color: #3d3d3d; font-weight: 400;">Do I first need to create a Layer from the item and then add that to the web map maybe?</SPAN>