I'm having some trouble with updating layers using the ArcGIS API for Python. I had updated a map image layer with some new sublayers, and wanted to make sure the new layers were applied everywhere the original MIL is used. I wrote a notebook to find and remove the instances of this MIL and re-add to the map. This worked for most of the maps, but enough didn't that I ended up having to check them all manually. I'll step through my code below if anyone can tell me if I'm missing something.
First, I listed all the web maps owned by our admin user and created an Item object for the MIL:
maps <SPAN class="operator token">=</SPAN> portal<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"type: Web Map NOT Application owner: GeoNexusAdmin@PENNICHUCK"</SPAN><SPAN class="punctuation token">,</SPAN> max_items <SPAN class="operator token">=</SPAN> <SPAN class="number token">100</SPAN><SPAN class="punctuation token">)</SPAN>
newLayer <SPAN class="operator token">=</SPAN> portal<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'id:b8c15eb2b33b4c0bbb7dfa0011cb28be'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Next, I iterated through the maps and looked for the MIL in question. If it was found, I removed and replaced the layer:
<SPAN class="keyword token">for</SPAN> m <SPAN class="keyword token">in</SPAN> maps<SPAN class="punctuation token">:</SPAN>
map <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>WebMap<SPAN class="punctuation token">(</SPAN>m<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#loop through layers in each map</SPAN>
layers <SPAN class="operator token">=</SPAN> map<SPAN class="punctuation token">.</SPAN>layers
<SPAN class="keyword token">for</SPAN> l <SPAN class="keyword token">in</SPAN> layers<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#store layer index number</SPAN>
layerNum <SPAN class="operator token">=</SPAN> map<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">(</SPAN>l<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#if a matching itemid is found, remove layer and add it back in</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="string token">'itemId'</SPAN> <SPAN class="keyword token">in</SPAN> layers<SPAN class="punctuation token">[</SPAN>layerNum<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> layers<SPAN class="punctuation token">[</SPAN>layerNum<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'itemId'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN><SPAN class="string token">'b8c15eb2b33b4c0bbb7dfa0011cb28be'</SPAN><SPAN class="punctuation token">:</SPAN>
map<SPAN class="punctuation token">.</SPAN>remove_layer<SPAN class="punctuation token">(</SPAN>map<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN>layerNum<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
map<SPAN class="punctuation token">.</SPAN>add_layer<SPAN class="punctuation token">(</SPAN>newLayer<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print map title to confirm</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>m<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">)</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Almost every map that was printed in the output list updated the MIL succesfully, but about 3 did not. Is this a glitch in the system, or do I need to make some tweaks to my code? At the very least, it would be nice to be able to identify and list the maps that did not update successfully.