How to successfully Edit Service Iteminfo from python script?

1056
1
10-17-2019 11:45 PM
AmrutaBildikar
New Contributor II

Hi All, 

I am working with ArcGIS Server 10.5.1 and ArcPy/python 2.7.1.

Operation which I want to perform:

Edit Item Information—ArcGIS REST API: Administer your server | ArcGIS for Developers 

I am trying to edit the iteminfo if a map service published on ArcGIS Server from python code. When I am trying to do the same manually with admin login, I am able to successfully edit the item info but when I am trying to do the same from python code, I get response as "success" but no change in item info no error as well but iteminfo is not getting updated!

Can anyone has faced the same issue or any guidance to do the same?

Please have a look at the function to update the item info:

def EditServiceMetadata(username, password, serverName, foldername, serviceName, iteminfo):
    '''
    This function edits/updates the metadata(service name, description, tags etc) from Map service URL.
    :param username: username.
    :type username: string
    :param password: password.
    :type password: string
    :param serverName: AGS server hostname.
    :type serverName: string
    :param foldername: folder Name published on AGS to pick up service from.
    :type foldername: string
    :param serviceName: mapservice Name published on AGS to update the metadata from.
    :type serviceName: string
    :param iteminfo: item info object holds the metadata of the service
    :type iteminfo: json string

    :returns: status of the operation post editing the iteminfo.
    :rtype: json
    '''

    AGS_updateserviceMetadata = "https://{}:6443/arcgis/admin/services/{}/{}.MapServer/iteminfo/edit?token={}&f=pjson"
    try:

        token = GetToken(username, password, serverName)
        updateserviceItemURL = (AGS_updateserviceMetadata.encode("utf-8")).format(serverName, foldername, serviceName,
                                                                                  token)
        inputItemInfo = json.dumps(iteminfo)
        testjson = {
            "Culture": "en-US",
            "name": "usa",
            "thumbnail": "thumbnail/image.jpg",
            "guid": "FD09F5FF-4031-49D4-8BD3-B310728C8FF7",
            "catalogpath": "",
            "snippet": "",
            "description": "",
            "summary": "",
            "title": "usa.mxd",
            "tags": "",
            "type": "Service Definition",
            "Text": "",
            "typekeywords": [
                "Shapefile Feature Class",
                "ArcGIS",
                "Service Definition",
                ".sd"
            ],
            "documentation": "",
            "url": "",
            "datalastmodifiedtime": "",
            "extent": {
                "xmin": -178.217598362366,
                "ymin": 18.9247817993163,
                "xmax": -66.9692710360024,
                "ymax": 71.4071353542713
            },
            "spatialreference": "Unknown",
            "accessinformation": "",
            "licenseinfo": ""
        }
      
        resp = requests.post(updateserviceItemURL, json={'iteminfo': testjson}, verify=False)
        if (resp.status_code != 200):
            print(time.ctime() + " Error while fetching tokens from admin URL. Please check the URL and try again.")
            return
        else:
            data = resp.json()
            if not assertJsonSuccess(data):
                return
            UpdateMetadatastatus = json.loads(data)
    except Exception as err:
        print(time.ctime() + unicode(err).encode("utf-8"))
    return UpdateMetadatastatus

Thank you for your help!

Regards,

Amruta

1 Reply
adrianakbari
New Contributor II

The documentation on the site (https://developers.arcgis.com/rest/enterprise-administration/server/edititeminfo.htm) is not correct. In your script instead of iteminfo you need to use serviceiteminfo.

        editSvcURL = "/arcgis/admin/services/" + service + "/iteminfo/edit"
        params = urllib.parse.urlencode({'token': token, 'f''json''serviceItemInfo': updatedSvcJson})
        httpsConn.request("POST", editSvcURL, params, headers)