TypeError: can only concatenate str (not "NoneType") to str

6663
28
Jump to solution
08-15-2022 08:02 AM
ABishop
MVP Regular Contributor

Hello,

I hope this is posted in the right place.  I am troubleshooting a script that I have been running for almost a year now which updates a hosted feature service in ArcGIS Online.  We recently upgraded our ArcMap and Server to 10.8.2 and ArcGIS Pro 3.0.1.  I am wondering if this update has something to do with it?  Anyhow, I have posted the python  code below and attached a pic of the TypeError I receive when I try to run the script from Python 3 IDLE.  Please help. 

import arcpy
import os, sys
from arcgis.gis import GIS

### Start setting variables
# Set the path to the project
prjPath = "I:\\users\\abishop\\PY\\FeatureServiceUpdates\\AgUseParcels\\AgUseParcels.aprx"

# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "Agricultural_Use_Parcels"
portal = "https://mcpafl.maps.arcgis.com/" # Can also reference a local portal
user = "***************"
password = "*************"

# Set sharing options
shrOrg = True
shrEveryone = False
shrGroups = ""

### End setting variables

# Local paths to create temporary content
relPath = sys.path[0]
sddraft = os.path.join(relPath, "AgUseParcels.sddraft")
sd = os.path.join(relPath, "AgUseParcels.sd")

# Create a new SDDraft and stage to SD
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)
arcpy.StageService_server(sddraft, sd)

print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)

# Find the SD, update it, publish /w overwrite and set sharing and metadata
print("Search for original SD on portal…")
sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]
print("Found SD: {}, ID: {} n Uploading and overwriting…".format(sdItem.title, sdItem.id))
sdItem.update(data=sd)
print("Overwriting existing feature service…")
fs = sdItem.publish(overwrite=True)

if shrOrg or shrEveryone or shrGroups:
    print("Setting sharing options…")
    fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

print("Finished updating: {} – ID: {}".format(fs.title, fs.id))

#provide input to stop script - uncomment input below if you need to see the script final output in the command window
input('Press ENTER to exit') 
Amanda Bishop, GISP
0 Kudos
28 Replies
BrianPangtay
Occasional Contributor

I've never used Truncate/Append on AGO...only on source data: PC and/or database.

Usually right-click on layer in ArcGIS Pro: Sharing>Overwrite map layer to update AGO.   

ABishop
MVP Regular Contributor

Yes, I have also always used the same method you describe above, to overwrite a map service manually.  But with  AGO, if you want to automate the process of overwriting the hosted feature layer using a python script, you can use the "truncate" and "append" tools on the hosted feature layers in your AGO content, then copy the python code to use in your script for automation.  Make sense?  Before, I used this method, I was using a script that had to log into AGO as the user, download the service definition file, etc.  

Amanda Bishop, GISP
0 Kudos
BrianPangtay
Occasional Contributor

Can you share that part of the script so I can compare it to your original script above?

ABishop
MVP Regular Contributor

Sure no problem!

print("Starting script... importing arcpy modules, starting timer, and setting local variables...")
# Process: import arcpy modules
import arcpy, os, sys
from arcpy import env
from datetime import datetime
import ctypes
ctypes.windll.kernel32.SetConsoleTitleW('AgUseParcelsFSUpdate')

# Process: start timer
startTime = time.perf_counter()

# Process: set local variables
AgUseParcelsFS = "https://services5.arcgis.com/zh4IrtLvSvA0sfub/arcgis/rest/services/AgUseParcels/FeatureServer/0"
AgUseParcelsFC = r"K:\GIS_TOOLS\DB\arcgis2.sde\mcpagis.DBO.AgUseParcels"
print("All set... starting truncate and append to update AgUseParcels hosted feature layer...")

# Process: truncate hosted feature layer
arcpy.management.TruncateTable(AgUseParcelsFS)
print("AgUseParcels hosted feature layer truncated")

# Process: append hosted feautre layer with local data
arcpy.management.Append(AgUseParcelsFC, AgUseParcelsFS, "TEST", None, '', '')
print("AgUseParcels hosted feature layer appended with AgUseParcels feature class in arcgis2.sde")

print("AgUseParcelsFSUpdate script completed successfully")

# Process: end timer
endTime = time.perf_counter()
print("Timer stopped")
elapsedTime = round((endTime - startTime) / 60, 2)
print("Script finished in {0} minutes".format(elapsedTime))

# Process: date and time stamp
date = datetime.now().strftime("%Y_%m_%d-%I:%M:%S_%p")
print("Date and time stamp: "+f"{date}")

# Process: provide input to stop script - uncomment input below if you need to see the script final output in the command window
input('Press ENTER to exit') 
Amanda Bishop, GISP
BrianPangtay
Occasional Contributor

That was so much simpler than the original script.

Thanks!

 

ABishop
MVP Regular Contributor

Yes, exactly!

Amanda Bishop, GISP
0 Kudos
Ramon_de_Leon
Occasional Contributor

I've been experiencing this same issue, after upgrading to ArcGIS Pro 3, and just like in Amanda's case I was using the workflow outlined at https://support.esri.com/en/technical-article/000023164  to overwrite and update my services in AGOL. 

Now it fails on one of my APRX and works for others, am hoping to avoid doing the truncate/append route as the APRX it is failing for me has a lot of layers and definition queries.

Still working with Esri tech support in solving the issue.

 

 

0 Kudos
BrianPangtay
Occasional Contributor

There is a bug in the ArcGIS API library that comes with ArcGIS Pro 3.x. I had the Service Definition located in a folder other than the root.:

  • Bug No: BUG-000152141
  • Synopsis: In ArcGIS API for Python 2.0.1, the update() method fails to update a service definition if the service definition is located in a folder other than the root folder in ArcGIS Online.
  • Status: In Review
0 Kudos
Ramon_de_Leon
Occasional Contributor

Thanks for the reply Brian, hope they get a fix sooner than later. 

I guess, I'll stick to manually overwriting for the time being.

0 Kudos
BrianPangtay
Occasional Contributor

If you have this problem , you can just move your Service Definition in AGO to the root folder. That simple fix will solve this error.