Forum Discussion

mmoales's avatar
mmoales
Ideator II
8 years ago

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?  

8 Replies

  • Hello, We recommend against modifying the related_id of a relationship directly. Instead, you should delete the existing relationship and then create a new relationship to the desired item. You can take a look at this blog post for an example of how to both delete and create an item through the REST API. Chris
    Christopher Gillis Aras Labs Software Engineer
  • Hello, We recommend against modifying the related_id of a relationship directly. Instead, you should delete the existing relationship and then create a new relationship to the desired item. You can take a look at this blog post for an example of how to both delete and create an item through the REST API. Chris
    Christopher Gillis Aras Labs Software Engineer
  • Hi Chris, is there a reason why it´s not recommended to change the related_id directly? I have a "replace this Part in the BOM with the latest generation of this Part" Action that can be used on BOM items. The Action will replace the related_id. I agree that for certain ItemTypes replacing the related_id may cause some problems, as changing the Relationshp often triggers additional Methods. I think ItemType File is one potential candidate that would cause us trouble. But is there something else that could go wrong, espcially considering the ItemType "Part BOM"? Angela
  • Hi Chris, If I try to delete the relationship to an existing file first, I still get the ItemIsNotLockedException. I am trying to send the delete request as shown in the documentation:
    To remove a relationship, the client sends a DELETE request to the $ref URL of the corresponding relationship property, passing the $id query string option: DELETE host/.../$ref
    Here is my request:
    DELETE host/.../Document File/$ref?$id=Document File('30776875F5674ECA8AE3ED803CA8B4B4')
    Here is the response:
    {
        "error": {
            "code": "SOAP-ENV:Server.ItemIsNotLockedException",
            "message": "Aras.Server.Core.ItemIsNotLockedException"
        }
    }
    Adding the lock action does not help. Mark
  • Okay. So it appears  you need to do an explicit lock call first: POST host/.../Document('178D8999B5B54EBB8F943E9934088A03') { "@aras.action": "lock" } After that, I can delete the relationship. Finally, I have to send an explicit unlock. It would be nice if there was a way to do all of this in a single call. Mark
  • Another issue is after you delete the relationship, the previous Document ID is no longer valid. So an additional query is needed to get the ID of the modified Document.
  • Hi Mark, If you know the ID of the Document File relationship, you could perform a single REST call like localhost/.../Document File('5B2466563B5547FAA8268EC4E5F4C5F8'). This will delete the relationship directly without updating the parent at all, so the ID will be the same. Please note that if you have version tracking enabled on your Document ItemType, this kind of REST call will not create a new version of the parent Document. Chris
    Christopher Gillis Aras Labs Software Engineer
  • 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