<jupyter_start><jupyter_text>Bulk update maps for Use in Collector / Field Maps This notebook allows the user to define a search string for webmaps and set the ability to "Use in Field Maps" and/or "Use in Collector". It will first search for maps based on a string you define, confirm these are the maps you'd like to update, and then update each for Collector / Field Maps use based on parameters you define. Additionally, this notebook can be imported in ArcGIS Online and used as a hosted notebook.<jupyter_code>from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS("home")
# gis = GIS("https://arcgis.com","<insert_user_here>", verify_cert=False)<jupyter_output><empty_output><jupyter_text>Please set the parameters below. Example parameters have been set for this notebook. Note that we will only search for maps we own in the example queriesIn this example query, we'll disable in Field Maps all webmaps that are currently not enabled for Collector. This will prevent field workers from accidentally using the wrong map!For more on writing this search query, please see: https://doc.arcgis.com/en/arcgis-online/reference/advanced-search.htm<jupyter_code># this takes "enabled", "disabled", or "no_change"
field_maps_usage = "enabled"
collector_usage = "no_change"
webmap_search_query = f"Hydrant NOT typekeywords: 'Collector' OR typekeywords: 'CollectorDisabled' AND owner:{gis.users.me.username}"

# Example query which would only update maps currently enabled for Collector
# webmap_search_query = f"title:Hydrant AND typekeywords:'Collector' AND owner:{gis.users.me.username}"

# Example query which would update maps currently enabled for Field Maps
# webmap_search_query = f"title:Hydrant AND NOT typekeywords:'FieldMapsDisabled' AND owner:{gis.users.me.username}"

valid_entries = ["enabled", "disabled", "no_change"]
if not field_maps_usage in valid_entries or not collector_usage in valid_entries:
    raise Exception("Check your usage parameters are set correctly")<jupyter_output><empty_output><jupyter_text>Now, let's search for the webmaps that we're going to update<jupyter_code>webmaps = gis.content.search(webmap_search_query, item_type='Web Map', max_items=1000, outside_org=False)
print("\n")
if len(webmaps) > 0:
        yn = (input(str(len(webmaps)) + ' web maps found. Are you sure you want to work on all these web maps? (y/N): ') or 'n').lower().strip()
        sure = yn in ['yes', 'y', 'ye']
else:
    raise Exception("No items found with query:", webmap_search_query)<jupyter_output><empty_output><jupyter_text>If you want to continue, we'll now update the keywords needed to use Collector and Field MapsNote here that we update the map once - once by calling `update()` on the item and once by calling `update()` on the WebMap. The second update call exists to scrub out keywords. So it's possible that your keywords are unchanged despite the modified date of the webmap changing.<jupyter_code>if not sure:
    raise Exception("Script terminated. Please check your search string again")
else:
    for wm_item in webmaps:
        print("Checking whether to update webmap for Field Maps and Collector: " + wm_item.title)
        current_keywords = set(wm_item.typeKeywords)
        if field_maps_usage == "enabled":
            current_keywords.discard("FieldMapsDisabled")
        elif field_maps_usage == "disabled":
            current_keywords.add("FieldMapsDisabled")

        if collector_usage == "enabled":
            current_keywords.discard("CollectorDisabled")
        elif collector_usage == "disabled":
            current_keywords.discard("Collector")
            current_keywords.add("CollectorDisabled")

        if sorted(list(current_keywords)) != sorted(wm_item.typeKeywords):
            print("New keyword added for " + wm_item.title + ". Updating webmap")
            try:
                wm_item.update(item_properties={"typeKeywords": list(current_keywords)})
            except Exception as e:
                print(e)
            wm = WebMap(wm_item)
            # remove bad Collector typekeywords
            wm.update()
        else:
            print("Skipped updating webmap for " + wm_item.title)
        print("\n")
            
print("Done!")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0881316098707403, 'fraction_numerical': 0.0011750881316098707, 'mean_word_length': 4.140096618357488, 'pattern_counts': {'":': 5, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '108664', 'license': 'Apache-2.0', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1170, 'n_words': 431, 'path': 'notebooks/Bulk Update Maps for Use in Collector or Field Maps.ipynb', 'repo_name': 'Esri/field-maps-scripts', 'size': '4255'}