Friday, June 23, 2017

Add/Remove aspects on a node in alfresco


There is an out of the box web script which Alfresco uses internally to add remove aspects. This web script can be seen in browser console when you go to "Manage Aspect" action and select a set of aspects to add/remove.

I came across an issue while working, where i had to remove a set of aspects and then add a set of aspects.

Initially i thought of writing a custom WebScript but got idea to see how "Manage Aspect" action is working? Can i utilize same back-end script to handle my case?

And guess what, it worked well :)

So let me give the little detail about the WebScript which Alfresco uses under "Manage Aspect" action.

WebScript:

[POST] http://<host:port>/share/proxy/alfresco/slingshot/doclib/action/aspects/node/{store_type}/{store_id}/{id}

where,

store_type: workspace
store_id: SpacesStore
id: nodeRefId (UUID of the node)

Just to set the context let me tell little bit about store_type, store_id and id.

store_type and store_id combined is called StoreRef. store_type , store_id and id will construct nodeRef.

A StoreRef is comprised of:

Store Protocol - that is, the type of store
Store Identifier - the id of the store

Example storeRefs are:

workspace://SpacesStore  (store_type://store_id)
version://versionStore  (store_type://store_id)
archive://SpacesStore   (store_type://store_id)

So, let's try out above given WebScript.

Suppose i have a node (workspace://SpacesStore/49cd3638-b0b2-40d8-ae14-a40cf7fad3b1) where i want to remove a set of aspects and then add another set of aspects.

We have to prepare URL like given below and send the POST request with below given JSON payload.

URL:

[POST] http://<host:port>/share/proxy/alfresco/slingshot/doclib/action/aspects/node/{store_type}/{store_id}/{id}


http://1270.0.0.1:8081/share/proxy/alfresco/slingshot/doclib/action/aspects/node/workspace/SpaceStore/49cd3638-b0b2-40d8-ae14-a40cf7fad3b1


To remove a set of aspects and add a set of aspects, we can use below given payload:
{
    "added":["pubModel:publishable", "pubModel:webable"],
    "removed":["cm:versionable", "exif:exif"]
}

To add a set of aspects, we can use below given payload:
{
    "added":["pubModel:publishable", "pubModel:webable"],
    "removed":[]
}

To remove a set of aspects, we can use below given payload:
{
   "added":[],
   "removed":["cm:versionable", "exif:exif"]
}

If operation is successful, then you will get below JSON response.
{
   "totalResults": 1,
   "overallSuccess": true,
   "successCount": 1,
   "failureCount": 0,
   "results":
   [
      {
         "id": "Assets",
         "action": "manageAspects",
         "nodeRef": "workspace:\/\/SpacesStore\/49cd3638-b0b2-40d8-ae14-a40cf7fad3b1",
         "type": "folder",
         "success": true
      }
   ]
}




If operation is unsuccessful (probably if aspect is invalid), then you will get below JSON response.
{
    "totalResults": 1,
    "overallSuccess": false,
    "successCount": 0,
    "failureCount": 1,
    "results": [
        {
            "id": "PO marc 3",
            "action": "manageAspects",
            "nodeRef": "workspace://SpacesStore/49cd3638-b0b2-40d8-ae14-a40cf7fad3b1",
            "type": "folder",
            "success": false
        }
    ]
}



Although above given WebScript is not the best way to do these operations, but as a workaround it is an option.