ArcGIS Blog

Mapping

ArcGIS Pro

Publish and overwrite web layers in ModelBuilder

By Jonah Lay

As we continue the fight against COVID-19 and enter the wildfire and hurricane season, web applications like dashboards help people make informed, time-sensitive, and even life-saving decisions. Depending on the events your organization is monitoring, things can change rapidly. You’ll need to efficiently publish your web layers, update your data, and overwrite your web layers to keep your dashboards current and relevant.

ModelBuilder in ArcGIS Pro allows you to automate analyzing and updating the data used in your web layers. You may already have a model you routinely run to prepare your data for publishing. All that remains is the ability to publish web layers or overwrite existing ones in the model. In this article, you’ll learn how to create a script tool in ArcGIS Pro 2.8 that publishes or overwrites a web feature layer. The script tool is built on ArcPy functions and geoprocessing tools, allowing you to run it like any other geoprocessing tool in new or existing ModelBuilder models.

Use a custom publishing script tool in ModelBuilder

You’ll be guided through each of the four steps below:

  1. Write a Python script. (Not too familiar with Python? Don’t worry; a sample script is provided that you can modify to meet your specific needs.)
  2. Create a script tool.
  3. Add the script tool to ModelBuilder.
  4. Run the model.

If you want to view the completed script tool and see how it works in an existing model, download this project to follow along.

Step 1: Write a Python script

The code below publishes a new web feature layer or overwrites an existing web feature layer in ArcGIS Online or ArcGIS Enterprise. Copy and paste the script in a text editor like Notepad and save it as a Python file (.py). This script can be modified to meet your specific needs. You’ll learn how to do this under Next Steps at the end of this article. If you would like to learn more about this publishing script and others like it, see Introduction to arcpy.sharing.

Step 2: Create a script tool

Next, follow all three sections below to create a script tool.

Create a custom toolbox to store your tool

1. Open your ArcGIS Pro project. In the Catalog pane, right-click Toolboxes and click New Toolbox.

Create a new toolbox

2. On the New Toolbox dialog box, provide a name for your toolbox and click Save.

Import the Python script

  1. In the Catalog pane, right-click the new toolbox you just created, and select New > Script.
  2. On the New Script dialog box, under General, provide the following:
    Name: PublishWebFeatureLayer
    Label: Publish Web Feature Layer
  1. Browse to the location of the Python script file you saved earlier. Select the file and click OK.
  2. Check Import script.
  3. Leave the Tool Properties dialog box open for the next section.
Script tool properties

Define the parameters of your script tool

Parameters are variables that serve as inputs for your script tool. When you run the tool, the Python script reads the parameter values before executing the rest of the script.

Under Parameters, add the following parameters with their properties:

Note: These properties can be modified based on your preferences. (For example, you can change the Share With Organization parameter so that it defaults to SHARE_ORGANIZATION instead of NO_SHARE_ORGANIZATION.)

0. Label: Map
    Name: in_map
    Data Type: Map
    Type: Required
    Direction: Input

1. Label: Web Layer Name
    Name: service
    Data Type: String
    Type: Required
    Direction: Input

2. Label: Summary
    Name: summary
    Data Type: String
    Type: Optional
    Direction: Input

3. Label: Tags
    Name: tags
    Data Type: String
    Type: Optional
    Direction: Input

4. Label: Description
    Name: description
    Data Type: String
    Type: Optional
    Direction: Input

5. Label: Overwrite Existing Web Layer
    Name: overwrite_existing_service
    Data Type: Boolean
    Type: Optional
    Direction: Input

6. Label: Enable Editing
    Name: enableEditing
    Data Type: Boolean
    Type: Optional
    Direction: Input
    Category: Configuration

7. Label: Enable Sync
    Name: enableSync
    Data Type: Boolean
    Type: Optional
    Direction: Input
    Category: Configuration

8. Label: Enable WFS
    Name: enableWFS
    Data Type: Boolean
    Type: Optional
    Direction: Input
    Category: Configuration

9. Label: Time zone
    Name: timezone
    Data Type: String
    Type: Optional
    Direction: Input
    Category: Configuration

10. Label: Share With Everyone
      Name: share_public
      Data Type: String
      Type: Optional
      Direction: Input
      Category: Share with
      Filter: Value List
                   PUBLIC
                   PRIVATE
      Default: PRIVATE

11. Label: Share With Organization
      Name: share_organization
      Data Type: String
      Type: Optional
      Direction: Input
      Category: Share with
      Filter: Value List
                   SHARE_ORGANIZATION
                   NO_SHARE_ORGANIZATION
      Default: NO_SHARE_ORGANIZATION

12. Label: Share With Groups
      Name: share_groups
      Data Type: String
      Type: Optional
      Direction: Input
      Category: Share with

13. Label: Output Directory
      Name: sddraft_output_directory
      Data Type: Folder
      Type: Required
      Direction: Input
      Category: Output

14. Label: Rest Endpoint
      Name: Rest_Endpoint
      Data Type: String
      Type: Derived
      Direction: Output

Script tool parameters

Now that you’ve imported your script and set parameters for your tool, click OK on the Tool Properties dialog box.

Learn more about setting script tool parameters

Let’s examine the script tool you created. Double-click your script tool to open it.

Publish Web Feature Layer script tool

Publish Web Feature Layer is now a custom geoprocessing tool that can be run like any other system geoprocessing tool. It’s ready to be added to a model.

Optional: Include metadata

When creating a custom geoprocessing tool, it’s recommended to include documentation that helps others use your tool.

Right-click your script and click Edit Metadata.

Enter tags, summary, and usage. Under Syntax, fill out the fields below and click Save.

in_map:

Dialog Explanation: Map to publish.

Scripting Explanation: Map object of the map to publish.

service:

Dialog Explanation: Enter the name of a new or existing web layer. If the name already exists, the web layer will be overwritten. Otherwise, a new web layer will be created.

Scripting Explanation: A string that represents the service name.

summary:

Dialog Explanation: Enter a short summary of the web layer.

Scripting Explanation: A string that represents summary.

tags:

Dialog Explanation: Enter keywords or terms that describe the web layer.

Scripting Explanation: A string that represents tags, with different tags separated by comma.

description:

Dialog Explanation: Enter an in-depth description of the item.

Scripting Explanation: A string that represents a description.

overwrite_existing_service:

Dialog Explanation: If checked and a web layer with the same name exists, it will be overwritten.

Scripting Explanation: A Boolean that determines whether to overwrite an existing web layer.

enableEditing:

Dialog Explanation: Allow users to edit the web layer.

Scripting Explanation: A Boolean that determines whether to enable editing.

enableSync:

Dialog Explanation: Disconnected editing with synchronization.

Scripting Explanation: A Boolean that determines whether to enable sync.

enableWFS:

Dialog Explanation: Include a WFS layer.

Scripting Explanation: A Boolean that determines whether to share a WFS layer as well.

timezone:

Dialog Explanation: Enter the time zone in which your date values are stored.

Scripting Explanation: A string to set the time zone.

share_public:

Dialog Explanation: Share web layer with the public.

Scripting Explanation: A string that determines if the layer is PUBLIC or PRIVATE.

share_organization:

Dialog Explanation: Share web layer with all authenticated users in your organization.

Scripting Explanation: A string that determines whether the layer is shared to the organization.

share_groups:

Dialog Explanation: Share web layer with groups you belong to and its members.

Scripting Explanation: List of existing groups that the layer is to be shared to.

sddraft_output_directory:

Dialog Explanation: The folder on disk where your .sddraft and .sd files will be stored.

Scripting Explanation: Output directory to create .sddraft and .sd files.

Step 3: Add the script tool to ModelBuilder

To use the tool in ModelBuilder, open an existing model or right-click the toolbox and select New > Model to create a new model. Right-click the script in your toolbox and click Add To Model (or drag it into your model).

Model with Publish Web Feature Layer as a custom geoprocessing tool

You can include other geoprocessing tools in your model. For example, you can set other processes as preconditions so that they execute before the web layer is overwritten. To learn more, see Use ModelBuilder.

Step 4: Run the model

Double-click the tool and fill out the fields. To overwrite a web layer, provide the name of the existing web layer and check the Overwrite Existing Web Layer check box. If the name already exists, the web layer will be overwritten. Otherwise, a new web layer will be created. Before running it, validate the model to ensure that it will successfully run by clicking ModelBuilder > Run > Validate. Once the model has been validated, click Run to run the entire model. You can also schedule a model to run repeatedly or at a designated time.

Run the model

After the model successfully runs, add the web layer to a map in ArcGIS Pro or open it in Map Viewer.

Next steps

In this article, you learned how to publish and overwrite web feature layers in ModelBuilder by creating and running a script tool in a model. One of the advantages of authoring your own script tool is that you can modify it to meet your specific needs. A couple of examples are provided below. If you want to learn more about automating sharing workflows in ArcGIS Pro, see Introduction to arcpy.sharing.

Let’s suppose you want to publish your web feature layer to a portal folder and allow users to export the data. This can be done by adding the portalFolder and allowExporting properties in the FeatureSharingDraft.

Right-click the script tool in your toolbox and click Edit. Modify the Python file as shown below.

Modify the Python file to include portal and export properties
Modify the Python file to include portal and export properties

Save the file. Right-click the script tool and click Properties. Add two more script tool parameters (6 and 11) for the new properties.

Add the new parameters to the script tool

Using what you’ve learned in the previous sections, you can create a script tool specific to map image layers. The code below publishes or overwrites a map image layer on ArcGIS Enterprise. Follow the same steps discussed previously: save the code as a new Python file (.py), create a script tool, import the .py file, and define script tool parameters. Add the script tool to ModelBuilder and run the model when you have configured it to meet your needs.

The need for readily available information during times of crisis makes your GIS work invaluable. In this article, we’ve discussed how to create and use script tools and ModelBuilder in ArcGIS Pro to efficiently publish and update your web layers. Learn more about how to share your work in ArcGIS Pro.

Share this article

Subscribe
Notify of
27 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Carissa Choong(@cchoongsidwell)
November 9, 2021 11:08 am

This script is going to be such a huge help but I am running into an error when I run the script as coped: it says that: name “GetParameter” is not defined. Is it because I am running ArcGIS Pro 2.4 perhaps? Thanks!

Katarina Hirai(@khiraisl)
November 18, 2021 11:56 am

Anyone try this? I can’t get the Time Zone to set correctly. After publishing, the feature service reflects UTC for our data’s date fields even though I set the “timezone” parameter to “Pacific Standard Time”.

Yuguang Liu(@yuguangl_uom)
January 23, 2022 7:55 pm

Hi there,

I have created the publish web feature layer, but the input is “map”. What I want to select and publish is a “layer” in the content pane. Could you please tell me how to solve the problem? Thanks in advance.

Best
Leo

James Shreeve(@jshreevesnorfolk)
January 31, 2022 6:08 am
Reply to  Jonah Lay

Hi Jonah, you sent me to this blog from another post last week (thank you!) and I’ve been working through the above and have come across the same problem as Leo. I’ve had a look at the code sample you supplied, is there a way to create a script tool using that code or does it need to be run as a standalone Python script? Or is there a way to alter the code for this blog to look at features rather than map files? Many thanks in advance

Robert Thomson(@miramichiadministrator)
January 24, 2022 12:22 pm

This is great but I can only get it to work if I leave share_groups blank or just use one group name. Otherwise I get 0x800 “This value is not a member of” Any ideas? I’m separating group names with commas. I’m running ArcGIS Pro 2.9.

Robert Thomson(@miramichiadministrator)
January 25, 2022 8:42 am
Reply to  Jonah Lay

That was all I needed, thanks.

Nicholas Gray(@graynic)
February 10, 2022 1:46 pm

Thank you very much for posting this! Is there a way to set the Portal connection where the data will publish to? It looks like it defaults to whatever Portal you are/were signed in to last. I have several Portal/ArcGIS Online connections in Pro but would like to specify which one it publishes too. I plan on setting this up as a scheduled task so I need to make sure it publishes to the right Portal. Thank you!

jonathan molineaux(@jonathan-molineaux_noaa)
February 24, 2022 5:13 am

Thank you for posting this article. I am having an issue with seeing my Portal connection in the output folder though. Is there a workaround for this? If I add the SignInToPortal ArcPy function, will this allow me to view the Portal Connection in my output folder?

Last edited 3 years ago by jonathan molineaux
Evan Marshall(@evan-marshallci-stpaul-mn-us_stpaul)
April 1, 2022 8:02 am

Hello, I implemented this script tool and generally it works well. We do have a weird issue where if we try to use it to overwrite an existing hosted table, it works, but converts the item to a hosted feature layer. We need these items to persist in AGO as hosted tables. Any thought to adding a parameter to tell it to overwrite as a table rather than a service? or choose no geometry type? thanks!

Wilson, Jared(@jcwilson4_ncdenr)
April 8, 2022 11:24 am

I’ve downloaded and used the model as it is used in the demonstration map and it uploaded the layer directly to my AGOL content pane, however, when I attempt to run the Publish Web Feature Layer in my own map, the tool runs for a very extended period of time seemingly without end. Is there an option I should be selecting that is different? I selected to run the tool exactly how I had it in the demo as well.

steve miller(@suasteve)
April 26, 2022 7:35 am

I’ve downloaded the script and got the Model working with one exception, I have a date field and I can’t get it to publish in Eastern Time Zone. What should I be putting in the Time zone parameter? I’ve tried EDT and Eastern_Daylight_Time, but the date field in the web layer is showing UTC. As a side note, when I query the AGOL rest the date field is in EDT.

Thank
Steve

Julia Hillin(@jhillin)
June 24, 2022 11:40 am

I’ve tried running this script but I get a traceback error to lines 30, 68, and 122 and a ValueError: Missing target server. I copied the script over directly and used the correct inputs/configurations, so I’m not sure what the issue is. Any ideas?

Last edited 3 years ago by Julia Hillin
Michael Olkin(@swscgisadmin)
November 2, 2022 9:45 am

I have successfully run this script within a model in a project, but I have not yet had success running it as a scheduled task. Here’s the error that I see in the log after attempting to run it as a scheduled task:

<msg code=”100″ type=”-2147467259″>Failed to execute. Parameters are not valid.</msg><msg code=”100″ type=”735″>The value is empty. ERROR 000735: Map: Value is required</msg>

I have checked and re-checked the script and parameters, but all appears to be set up correctly.

Nicholas Gray(@graynic)
December 13, 2024 9:41 am
Reply to  Michael Olkin

@Michael Olkin did you ever fix this issue? I am experiencing the same thing. I can run the model fine and it publishes but when I schedule it I get this same error. @Jonah Lay Thank you!

Amin Alhassan(@amin-alhassanloudoun-gov_loudounps)
August 9, 2023 10:18 am

Hello Jonah,
Thanks for sharing this script, it’s still “golding” to me. I have adapted it and tried to set the time zone to Eastern Standard Time or ET,EST and all the ways I can imagine the eastern standard time; yet, the time aware data after publishing still shows UTC time values. I have read others comments on the same issue. Are you able to look into that or any other work around. I really appreciate the effort put in.

Thanks,
Amin

Amin Alhassan(@amin-alhassanloudoun-gov_loudounps)
August 15, 2023 10:36 am
Reply to  Jonah Lay

Thanks Jonah, it worked at last. Any thought on overwriting a stand alone hosted feature layer in AGOL? This did not set the time zone but the feature was overwritten when I tried on a hosted feature layer not in a web map.

Carlos Alberto Duarte Carranza(@caduarte_vrip)
September 25, 2023 11:46 am

Dear Jonah,
Thanks for this great and absolutely useful tutorial!
I have a model which ends with a feature layer as output. How can I connect my model with the model in your tutorial to publish (only) my output feature to AGOL?

Thanks in advance!

Carlos

Andrew Ferguson(@andrew-ferguson_whg)
October 9, 2023 3:31 am

Dear Jonah, Thank you. This is an excellent article. I’m having some difficulty publishing a single feature via a model I’ve created. I’ve modified the code to select my layer with “selected_layer = m.listLayers()[0]” and provided this variable within a list as a parameter in the m.getWebLayerSharingDraft() function however I am getting the error: Error 001272: Analzer errors were encountered ([{"code":"00102", "message":"Selected layer does not contain a required layer type for web feature layer", "object":"Map"}]). I can manually publish the layer to AGOL without any analyser errors though. Can you offer any insight as to why this is happening? Thanks… Read more »

Adam Hart(@ahart_midlandcounty)
July 3, 2024 11:25 am

Jonah,

This is a great tool and I am hoping to use it with some of our data. I have one question…How would the code be modified to overwrite a layer with attachments? The steps in the model work fine to add the attachments to the feature class, but I’m having trouble trying to overwrite the AGO layer with the updated attachments. Any help would be greatly appreciated. Thanks