Select to view content in your preferred language

How do I set dynamic layers on a map service with ArcPy

4083
11
01-25-2016 12:04 PM
JacobBoyle
Deactivated User

I've been writing a script to publish services, and I am getting stuck on setting dynamic layers on a map service in Manager

Enabling dynamic layers on a map service in Manager—Documentation (10.3 and 10.3.1) | ArcGIS for Ser...

I'd like to add this as a SOE I'm assuming to my script, but i'm not having any luck finding anything on how to do so.

0 Kudos
11 Replies
BillDaigle
Frequent Contributor

We use scripts (python toolbox) to publish our map and geoprocessing services.

For most of our services, we use the default service setting.  In order to set custom properties (like dynamic layers), we store the custom properties in a json config file. After the service is published, we fetch the service properties using the a call to the Admin API, edit the fetched json object using the stored file, then apply the edits by making another call to the Admin API.  

BillDaigle
Frequent Contributor

BTW, our server is still at 10.1, but I don't see any reason why it wouldn't apply to 10.3.

0 Kudos
JacobBoyle
Deactivated User

Yeah, I'm not sure this would work for 10.3.1.

10.3 uses the .mxd --> .sddraft --> .sd --> Service publishing path logic, not .mxd --> .msd --> .sd --> Service like 10.1 does.

Thanks though

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Jacob,

Not sure if you saw this link with some samples.  You can edit the .sddraft file using the xml.dom.minidom library.  I was able to figure out how to enable dynamic layers, but have not been successful is setting the dynamic workspaces.  Below is an example:

sddraft = r"E:\Temp\Python\Airports.sddraft"
doc = DOM.parse(sddraft)

# edit configuration properties
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 == "enableDynamicLayers":
                keyValue.nextSibling.firstChild.data = "true"
            if keyValue.firstChild.data == "dynamicDataWorkspaces":
                #the below would fail
                keyValue.nextSibling.firstChild.data = '[{"id":"MyFileGDBWorkspaceID","workspaceFactory":"FileGDB","workspaceConnection":"DATABASE=E:\\Temp\\Python\\Test.gdb"}]'
                

# output to a new sddraft
outXml = r"E:\Temp\Python\Airports_New.sddraft"   
if os.path.exists(outXml): os.remove(outXml)
f = open(outXml, 'w')     
doc.writexml( f )     
f.close()

Hopefully a python expert can way in to see how to update the 'dynamicDataWorkspaces' portion of the sddraft file.

JacobBoyle
Deactivated User

Yeah that's the only part I've struggled with, I've setup a script that will disable KML, enable Feature Services and set Feature services to Query only in the .sddraft file prior to publishing.  I just can't get the syntax to Enable Dynamic Workspaces.

0 Kudos
BillDaigle
Frequent Contributor

.mxd --> .sddraft --> .sd --> Service

We're at 10.1 SP1, and the work flow is the same. .mxd --> .sddraft --> .sd --> Service.

Regardless, what I'm suggest occurs after the service is published.  I will see if I can scrape together a sample script.

0 Kudos
BillDaigle
Frequent Contributor

Here is a code sample demonstrating how to update service parameters after the service has been published.

#modify these to match your environment
serverHost = 'yourgisServer.com'
serverPort = 6080
serviceRelativePath = r'/services/serviceFolder/serviceName.MapServer'
username = 'yourAdminName'
password = 'yourAdminPW'

# properites you would like to apply to the service
newProperties = {
 "minInstancesPerNode": 1,
 "maxInstancesPerNode": 5,
 "properties": {
  "enableDynamicLayers": "true",
  "dynamicDataWorkspaces": "[{\"id\":\"wsName\",\"workspaceFactory\":\"SDE\",\"lockVersion\":\"false\",\"workspaceConnection\":\"connectionProps\"}]"
 }
}


import json
import urllib
import httplib


# ####################
# helper functions
# ####################
def getUpdatedDict(inDict,modDict):
    def _getPathsAndValuesFromModDict(d,parentDict):
        for k, v in d.iteritems():
            if isinstance(v, dict):
                _getPathsAndValuesFromModDict(v,parentDict)
            else:
                parentDict = v
    _getPathsAndValuesFromModDict(modDict,inDict)
    return inDict


def adminApiCall(strAdminApiUrl,objParams):

    def _getToken():

        tokenURL = "/arcgis/admin/generateToken"
        params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        httpConn = httplib.HTTPConnection(serverHost, serverPort)
        httpConn.request("POST", tokenURL, params, headers)
        response = httpConn.getresponse()
        if (response.status != 200):
            httpConn.close()
            return
        else:
            data = response.read()
            httpConn.close()
            # Check that data returned is not an error object
            if not _assertJsonSuccess(data):
                return
            # Extract the toke from it
            token = json.loads(data)
            return token['token']

    def _assertJsonSuccess(data):
        obj = json.loads(data)
        if 'status' in obj and obj['status'] == "error":
            #print "Error: JSON object returns an error. " + str(obj)
            return False
        else:
            return True

    token = _getToken()
    if objParams:
        objParams['token'] = token
        objParams['f'] = 'json'
    else:
        objParams = {'token': token,'f': 'json'}

    params = urllib.urlencode(objParams)
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

    # Connect to URL and post parameters
    httpConn = httplib.HTTPConnection(serverHost, serverPort)
    httpConn.request("POST", r'/arcgis/admin'+ strAdminApiUrl, params, headers)

    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        print response.status
        httpConn.close()
        output = "Error: Code "+str(response.status)+', '+str(response.reason)
    else:
        data = response.read()

        # Check that data returned is not an error object
        if not _assertJsonSuccess(data):
            obj = json.loads(data)
            output =  obj['messages']
        else:
            dataObj = json.loads(data)
            output =  dataObj
    httpConn.close()
    try:
        if output[0] == u'Authorization failed. This user account does not have the required privileges to access this application.':
            return {'status':'error','msg':u'Authorization failed for user'}
    except:
        return output
# ####################
# end helper functions
# ####################


#fetch the parameters using the admin api
serviceInfo = adminApiCall(serviceRelativePath,{'f':'json'})



#update new parameters using the new properties
serviceInfo = getUpdatedDict(serviceInfo,newProperties)

#apply the updated params
adminApiCall('/services/test/testForDave.MapServer/edit',{'f':'json','service':json.dumps(serviceInfo)})

Modifying properties after the service is published is, in my opinion, easier than doing so via the sd or sddraft.

JacobBoyle
Deactivated User

I think this is going to the me in the right direction, I'm trying to achieve the same with a python script.  I'm trying to make it easier to ensure my GIS users are publishing new services.  I've figured out the syntax for everything else though.

0 Kudos
JacobBoyle
Deactivated User

Can't figure out how to edit my comments, I'm trying to do the same in a python script that you use on a new service. 

0 Kudos