Select to view content in your preferred language

Setting the Time Zone when publishing Map Service via ArcPy?

5296
12
08-02-2019 04:37 PM
RandyKreuziger
Frequent Contributor

We are using a bare bones ArcPy Script from the ESRI site to publish Map Services from ArcGIS Pro projects.  When publishing, the default Time Zone is none but we want it changed to Pacific Time with values adjusted for daylight savings.  Is there a way to do this when using arcpy when publishing?

for m in aprx.listMaps():
    if mapNames == "ALL" or mapNames == m.name:
        AddMsgAndPrint("Map: " + m.name)
        serviceName = m.name    
        SDPath = os.path.join(tempPath, serviceName + ".sd")
    
        # Create MapServiceDraft and set service properties
        sddraft = arcpy.sharing.CreateSharingDraft('STANDALONE_SERVER', 'MAP_SERVICE', serviceName, m)
        sddraft.targetServer = server_con
        sddraft.serverFolder = serverFolder
        # copyDataToServer = False will reference data from registered data sources with the targetServer
        sddraft.copyDataToServer = False
        sddraft.exportToSDDraft(SDdraftPath)
        
        # Read the contents of the original SDDraft into an xml parser
        doc = DOM.parse(SDdraftPath)
        
        # The follow code piece modifies the SDDraft from a new MapService with caching capabilities
        # to a FeatureService with Map, Create and Query capabilities.
        typeNames = doc.getElementsByTagName('TypeName')
        for typeName in typeNames:
            if typeName.firstChild.data == "FeatureServer":
                extention = typeName.parentNode
                for extElement in extention.childNodes:
                    if extElement.tagName == 'Enabled':
                        extElement.firstChild.data = 'false'
        
        # Turn off caching
        configProps = doc.getElementsByTagName('ConfigurationProperties')[0]
        propArray = configProps.firstChild
        propSets = propArray.childNodes
        for propSet in propSets:
            keyValues = propSet.childNodes
            for keyValue in keyValues:
                if keyValue.tagName == 'Key':
                    if keyValue.firstChild.data == "isCached":
                        keyValue.nextSibling.firstChild.data = "false"
        
        # Write the new draft to disk
        f = open(newSDdraftPath, 'w')
        doc.writexml(f)
        f.close()
        
        # Stage the service
        arcpy.StageService_server(newSDdraftPath, SDPath)
        AddMsgAndPrint("  Staged service")
        
        # Upload the service
        arcpy.UploadServiceDefinition_server(SDPath, server_con)
        AddMsgAndPrint("  Uploaded service")
        
        # Delete drafts
        Cleanup(SDdraftPath)
        Cleanup(newSDdraftPath)
        Cleanup(SDPath)
        AddMsgAndPrint("  Removed temp files")
12 Replies
AngelaDeegan
Frequent Contributor

Would love to have an answer to this question. Since enabling editor tracking in our Enterprise database (using database time)  we are unable to get the arcpy.StageService_server command to run successfully.  We can publish a map just fine via the ArcGIS Pro GUI (with time set to UTC-08:00 and the Daylight savings time box checked). But when we try to automate this using a script, it fails every time with the following error:

arcgisscripting.ExecuteError: ERROR 001272: Analyzer errors were encountered ([{"code":"00129","message":"Map has layers and tables that record Editor Tracking dates in database time zone, but time zone has not been defined","object":"Viewer - Meter Reading"}]).

Failed to execute (StageService).

0 Kudos
JulieEckman1
Occasional Contributor

I would like an answer to this issue also.  I have editor tracking enabled in my feature table and its two associated relation tables being used in ArcGIS Pro.  Even though I set up the feature table's time with Pacific, I can't seem to do the same for the relation tables (no time available in their properties).  I am trying to create a web map in either ArcGIS Online or in our own portal and I run into the same error which needs to be fixed before it can be shared as a web map.  Removing the tracking fields is not an option.

GaryChristensen1
Occasional Contributor

Angela,

Did you find an answer or work around to this? I would also like to overwrite a Feature Service while keeping the editor tracking enabled.

ThomasColson
MVP Frequent Contributor

Did you figure this out? We have a script to auto-update a service where display of time stamps in the correct time zone is critical. 

0 Kudos
ThomasColson
MVP Frequent Contributor

ENH-000128370: Provide a timezone parameter when sharing as a web layer using ArcPy

WilliamCraft
MVP Regular Contributor

I was able to accomplish setting the Time Zone name (i.e., the 'dateFieldsTimezoneID' property) using the script below.  I also set the 'dateFieldsRespectsDayLightSavingsTime' property to True at the same time.  This script iterates through all map services and makes the adjustments, but you could adapt this for publishing as well.  

import requests, urllib, ssl, json, arcpy, sys

context = ssl._create_unverified_context()
baseURL = "https://<server_name>:6443/arcgis"
userName = "admin"
passWord = "password"
timeZone = "Central Standard Time"

# Get the token
parameters = urllib.parse.urlencode({'username' : userName,
                               'password' : passWord,
                               'client' : 'requestip',
                               'expiration': 60,
                               'f' : 'json'}).encode("utf-8")
response = urllib.request.urlopen(baseURL + '/admin/generateToken?',parameters, context=context).read()
jsonResponse = json.loads(response)
token = jsonResponse['token']

# Loop through services within folders and set Time Zone value
params = {'f': 'json', 'token': token}
req = urllib.request.Request(baseURL + '/admin/services?f=pjson', urllib.parse.urlencode(params).encode("utf-8"))
response = urllib.request.urlopen(req, context=context)
data = json.load(response)
for d in data['foldersDetail']:
    folderName = d['folderName']
    if folderName != 'System' and folderName != 'Utilities':
        req = urllib.request.Request(baseURL + '/admin/services/' + folderName + '?f=pjson', urllib.parse.urlencode(params).encode("utf-8"))
        response = urllib.request.urlopen(req, context=context)
        data = json.load(response)
        for i in data['services']:
            serviceName = i['serviceName']
            arcpy.AddMessage("Modifying " + folderName + "/" + serviceName + "...")
            req = urllib.request.Request(baseURL + '/admin/services/' + i['folderName'] + '/' + serviceName + '.MapServer?f=pjson', urllib.parse.urlencode(params).encode("utf-8"))
            response = urllib.request.urlopen(req, context=context)
            data = json.load(response)
            try:
                dateFieldsTimezoneID  = data['properties']['dateFieldsTimezoneID']
            except:
                dateFieldsTimezoneID = ""
            data["properties"]["dateFieldsTimezoneID"] = timeZone
            data["properties"]["dateFieldsRespectsDayLightSavingTime"] = "true"
            updatedSvcJson = json.dumps(data)
            editSvcURL = baseURL + '/admin/services/' + i['folderName'] + '/' + serviceName + '.MapServer/edit'
            headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
            params = urllib.parse.urlencode({'token':token, 'f': 'json', 'service': updatedSvcJson})
            r = requests.post(editSvcURL, params, headers = headers, verify = False)

# Loop through services within the root folder and set Time Zone value
params = {'f': 'json', 'token': token}
req = urllib.request.Request(baseURL + '/admin/services?f=pjson', urllib.parse.urlencode(params).encode("utf-8"))
response = urllib.request.urlopen(req, context=context)
data = json.load(response)
for i in data['services']:
    if i['type'] == 'MapServer':
        serviceName = i['serviceName']
        arcpy.AddMessage("Modifying " + "root/" + serviceName + "...")
        req = urllib.request.Request(baseURL + '/admin/services/' + serviceName + '.MapServer?f=pjson', urllib.parse.urlencode(params).encode("utf-8"))
        response = urllib.request.urlopen(req, context=context)
        data = json.load(response)
        try:
            dateFieldsTimezoneID  = data['properties']['dateFieldsTimezoneID']
        except:
            dateFieldsTimezoneID = ""
        data["properties"]["dateFieldsTimezoneID"] = timeZone
        data["properties"]["dateFieldsRespectsDayLightSavingTime"] = "true"
        updatedSvcJson = json.dumps(data)
        editSvcURL = baseURL + '/admin/services/' + serviceName + '.MapServer/edit'
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        params = urllib.parse.urlencode({'token':token, 'f': 'json', 'service': updatedSvcJson})
        r = requests.post(editSvcURL, params, headers = headers, verify = False)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
MollyWatson1
Frequent Contributor

I've tried using this example to set the time zone property for some feature services in AGOL, but I continue to get a HTTP Error 404: Not Found. Has anybody else who has used this code example experienced the 404 error and were able to troubleshoot it? 

0 Kudos
WilliamCraft
MVP Regular Contributor

Is the 404 error in reference to the baseURL value?  Post a screenshot of the full error message.  My example above assumes you're using SSL so the URL is prefixed with HTTPS.  Is it possible you need to use HTTP with port 6080 instead?

0 Kudos
MollyWatson1
Frequent Contributor

Yes, I think the 404 issues was related to the baseURL value. I was actually able to find another work around so I don't need to edit the time zone properties in AGOL. We added a time stamp to our date values. When our data is uploaded to AGOL, it retains the current date instead of going backwards one date due to the difference in hours from UTC and MST. Our data just needs to have the correct date, so this process works for us for now. 

0 Kudos