Disable schema locking on multiple published services

1126
2
06-28-2019 06:41 AM
StephanieRinguet2
New Contributor III

We have hundreds of map services published on our server and they all have the default setting of schema locking enabled. We want to disable schema locking on all of them. We know we can change the service properties for each service either through ArcGIS Server or ArcMap, but is there a way to automate this process through scripting so we don't have to do this for each service individually?

2 Replies
AdrienLepoutre
New Contributor III

Hello,

You can do it via the REST API. Most of the documentation out there is written for Python 2.7.

Here is a code  in Python 3. You can add a loop to switch the serviceURL, that should do it.

import urllib, json
import urllib.request
import requests


def main():

    portalURL = r''
    username = ''
    password = ''
    new_token = generateToken(username, password, portalURL)

    # edit a service
    serviceURL = '<server root url>/admin/services/<service_name>.MapServer'
    params = {'f': 'json', 'token': new_token}
    req = urllib.request.Request(serviceURL, urllib.parse.urlencode(params).encode("utf-8"))
    response = urllib.request.urlopen(req)
    data = json.load(response)
    data["properties"]["schemaLockingEnabled"] = "false"

    # Serialize back into JSON
    updatedSvcJson = json.dumps(data)

    # Call the edit operation on the service. Pass in modified JSON.
    editSvcURL = serviceURL + "/edit"
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    params_e = urllib.parse.urlencode({'token':new_token, 'f': 'json', 'service': updatedSvcJson})
    r = requests.post(editSvcURL, params_e, headers = headers)


def generateToken(username, password, portalUrl):
    '''Retrieves a token to be used with API requests.'''
    parameters = urllib.parse.urlencode({'username' : username,
                                   'password' : password,
                                   'client' : 'referer',
                                   'referer': portalUrl,
                                   'expiration': 60,
                                   'f' : 'json'}).encode("utf-8")
    response = urllib.request.urlopen(portalUrl + '/sharing/rest/generateToken?',parameters).read()

    try:
        jsonResponse = json.loads(response)
        if 'token' in jsonResponse:
            return jsonResponse['token']
        elif 'error' in jsonResponse:
            print(jsonResponse['error']['message'])
            for detail in jsonResponse['error']['details']:
                print(detail)
    except ValueError:
        print('An unspecified error occurred.')


if __name__ == '__main__':
    '''
    debug
    '''
    main()
StephanieRinguet2
New Contributor III

Thank you.

0 Kudos