Disable Schema locks on multiple services

99
2
11-30-2021 09:53 AM
TonyAlmeida
Occasional Contributor

I need to disable the schema locks from 30 map services and can do it through Server one by one but that would take a long time. I have the following but nothing happens the services don't get schema locks don't get disabled.

 

Code:

 

 

import urllib, json
import urllib.request
import requests


def main():

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

    # edit a service
    serviceURL = '***/arcgis/admin/***.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
    '''

 

 

:

0 Kudos
2 Replies
jcarlson
MVP Frequent Contributor

I would strongly suggest using the ArcGIS Python API for this. Using the arcgis.gis.server module, you can update the service JSON. Combined with a few other classes in the same module, you can easily search for or list multiple services, then update their JSON iteratively.

- Josh Carlson
Kendall County GIS
TonyAlmeida
Occasional Contributor

Thank you for the suggestion but I would like to stay with python. Python API would be a total different world for me. Do you know of an example of what I am wanting to do with Python API ?

0 Kudos