Update relationship via REST API

オフライン
I'm trying to change the file that a Document/Document File relationship is using with the REST API. According to the documentation:
In order to set a value for an Item property, the client must send a PUT request to the Item property reference:
PUT host/.../$ref
Prefer: return=minimal
{
"@odata.id": "">host/.../User(‘C542FC153AE647A59CD6F6967295EF6B’)"
}
Here is how I am trying to do this for the Document File relationship:
PUT host/.../Document File('7E15...F5E9')/related_id/$ref
{
"@odata.id": "">host/.../File('0284...3BA0')"
}
This results in an Aras.Server.Core.ItemIsNotLockedException.  I also tried adding the @aras.action:lock to the JSON and this gave the same error. Is this the correct way to modify the Document File relationship?  
Parents
  • Hi Chris, That worked perfect for the delete. To create the relationship, I am first locking the Document, creating the Document File relationship, querying the Document for its new ID, and then unlocking the Document:
    def attach_file(s, keyed_name, fileId, item_id, item_type='Document', rel_type='Document File'):
        # lock
        data = { '@aras.action': 'lock' }
        r = s.post(
           host + "/InnovatorServer/Server/odata/{0}('{1}')".format(item_type, item_id),
           headers = REST_HEADERS,
           data=json.dumps(data))
        r.raise_for_status()
        # Create relationship
        data = { '[email protected]': "File('{0}')".format(fileId) }
        r = s.post(
            host + "/InnovatorServer/Server/odata/{0}('{1}')/{2}".format(item_type, item_id, rel_type),
            headers = REST_HEADERS,
            data=json.dumps(data))
        r.raise_for_status()
        # Get the updated item
        r = s.get(
            host + "/InnovatorServer/Server/odata/{0}?$filter=keyed_name eq '{1}'".format(item_type, keyed_name),
            headers = REST_HEADERS)
        r.raise_for_status()
        item_id = r.json()['value'][0]['id']
        # Unlock
        data = { '@aras.action': 'unlock' }
        r = s.post(
            host + "/InnovatorServer/Server/odata/{0}('{1}')".format(item_type, item_id),
            headers = REST_HEADERS,
            data=json.dumps(data))
        r.raise_for_status()
    Is there a cleaner way to do this? Thanks, Mark
Reply
  • Hi Chris, That worked perfect for the delete. To create the relationship, I am first locking the Document, creating the Document File relationship, querying the Document for its new ID, and then unlocking the Document:
    def attach_file(s, keyed_name, fileId, item_id, item_type='Document', rel_type='Document File'):
        # lock
        data = { '@aras.action': 'lock' }
        r = s.post(
           host + "/InnovatorServer/Server/odata/{0}('{1}')".format(item_type, item_id),
           headers = REST_HEADERS,
           data=json.dumps(data))
        r.raise_for_status()
        # Create relationship
        data = { '[email protected]': "File('{0}')".format(fileId) }
        r = s.post(
            host + "/InnovatorServer/Server/odata/{0}('{1}')/{2}".format(item_type, item_id, rel_type),
            headers = REST_HEADERS,
            data=json.dumps(data))
        r.raise_for_status()
        # Get the updated item
        r = s.get(
            host + "/InnovatorServer/Server/odata/{0}?$filter=keyed_name eq '{1}'".format(item_type, keyed_name),
            headers = REST_HEADERS)
        r.raise_for_status()
        item_id = r.json()['value'][0]['id']
        # Unlock
        data = { '@aras.action': 'unlock' }
        r = s.post(
            host + "/InnovatorServer/Server/odata/{0}('{1}')".format(item_type, item_id),
            headers = REST_HEADERS,
            data=json.dumps(data))
        r.raise_for_status()
    Is there a cleaner way to do this? Thanks, Mark
Children
No Data