Select to view content in your preferred language

Setting the Time Zone when publishing Map Service via ArcPy?

4753
12
08-02-2019 04:37 PM
RandyKreuziger
Regular 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
RandyKreuziger1
Occasional Contributor

Original poster here, we are still looking for a way to do this via arcpy script during publishing.  

James_Norquest
New Contributor II

Got this figured out with the help of a coworker and ESRI. Sorry about the spacing, ill see if I can attach a file with the correct indents.

 

import os

import arcpy
from arcgis.gis import GIS
import xml.dom.minidom as DOM

# Function to configure properties of an extension
# soe = extension for which properties have to be added


def enable_configproperties(sddraftPath, soe, property_set):


# Read the sddraft xml.
doc = DOM.parse(sddraftPath)

# Find all elements named TypeName. This is where the server object extension
# (SOE) names are defined.
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName we want to enable.
if typeName.firstChild.data == soe:
extension = typeName.parentNode
# prp = extension.childNodes.getElementsByTagNameNS('PropertyArray')
for extElement in extension.childNodes:
if extElement.tagName == 'Definition':
for definition in extElement.childNodes:
if definition.tagName == 'ConfigurationProperties':
for config_prop in definition.childNodes:
if config_prop.tagName == 'PropertyArray':
for prop in property_set:
prop_set = doc.createElement("PropertySetProperty")
attr = doc.createAttribute("xsi:type")
attr.value = "typens:PropertySetProperty"
prop_set.setAttributeNode(attr)

prop_key = doc.createElement("Key")
txt = doc.createTextNode(prop["key"])
prop_key.appendChild(txt)
prop_set.appendChild(prop_key)

prop_value = doc.createElement("Value")
attr = doc.createAttribute("xsi:type")
attr.value = "xs:string"
prop_value.setAttributeNode(attr)
txt = doc.createTextNode(prop["value"])
prop_value.appendChild(txt)
prop_set.appendChild(prop_value)

config_prop.appendChild(prop_set)

# Write to sddraft
f = open(sddraftPath, 'w')
doc.writexml(f)
f.close()



# Specify the location of the project file
prjPath = r"C:\Users\jnorquest\Documents\ArcGIS\Projects\Overwrite\Overwrite.aprx"

# Specify the feature service name in ArcGIS Online, including the owner credentials.
sd_fs_name = "Potable"
portal = "http://www.arcgis.com"
user = "******"
password = "******"

# Set the desired sharing options. The following code sample sets the service to be shared
# only with the organization.

shrOrg = True
shrEveryone = False
shrGroups = ""

# Specify a local path for storing temporary contents to be used for publishing the
# service definition draft and service definition file.
relPath = r'C:\Users\jnorquest\Documents\ArcGIS\Projects\Overwrite'
sddraft = os.path.join(relPath, "ProjectsWUDtest.sddraft")
sd = os.path.join(relPath, "ProjectsWUDtest.sd")

# Create a new SDDraft file, and stage the draft to the SD file.
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]

sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name)
sharing_draft.summary = "My Summary"
sharing_draft.tags = "My Tags"
sharing_draft.description = "My Description"
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"

sharing_draft.exportToSDDraft(sddraft)

# Set timezone
property_set = [{
"key": "dateFieldsRespectsDayLightSavingTime",
"value": "true"
},
{
"key": "dateFieldsTimezoneID",
"value": "UTC"
}]
# To set time zone on hosted feature service, soe = "FeatureServer"
enable_configproperties(sddraft, soe="FeatureServer", property_set=property_set)

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…")
print(f"Query: {sd_fs_name}")
sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition")
i=0
while sdItem[i].title != sd_fs_name:
i += 1
print('Item Found')
print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}')
item = sdItem[i]
item.update(data=sd)

print("Overwriting existing feature service…")
fs = item.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))

0 Kudos
James_Norquest
New Contributor II
 
0 Kudos