Monday, November 30, 2015

Get full asset details

There are times when we need to inspect all the fields of asset or either to perform other tasks. One can find all the details of any asset using the following method:

Using Tag:

<asset:load name="thisAsset" objectid='<%= ics.GetVar("cid")%>' type='<%= ics.GetVar("type")%>'/>
<asset:scatter name="thisAsset" prefix="thisAsset"/>
<%
for (Enumeration e = ics.GetVars(); e.hasMoreElements();) {
String varName = (String)e.nextElement();
if (varName.startsWith("thisAsset:")){
out.println("VarName: " + varName + " = " + ics.GetVar(varName));
}
}
%>
Using Asset API

<%
Session sess = SessionFactory.getSession(ics);
long assetid = Long.parseLong(ics.GetVar("cid"));
String assettype = ics.GetVar("c");
out.println("reading asset " + assetid + " of type " + assettype + "<p/>");
AssetId aid = new AssetIdImpl(assettype, assetid);
AssetDataManager adm = (AssetDataManager) sess.getManager(AssetDataManager.class.getName());
Iterable <AssetData > assets = adm.read(Collections.singletonList(aid));
for (AssetData a: assets) {
out.println("found " + a.getAssetId() + " of type " + a.getAssetTypeDef().getName() + "<br/>");
// get all core field + attributes
for (AttributeData attr : a.getAttributeData()) {
out.println(" found attribute: " + attr.getAttributeName() + " = " + attr.getData() + "<br/>");
}
// get immediate parents
for (AssetId imparents : a.getImmediateParents()) {
out.println(" found immediate parent: " + imparents.getType() + ":" + imparents.getId() + "<br/>");
}
// get parents
for (AssetId parents : a.getParents()) {
out.println(" found parent: " + parents.getType() + ":" + parents.getId() + "<br/>");
}
// get associations
for (AssetAssociationDef assoc : a.getAssetTypeDef().getAssociations()) {
out.println(" found assoc: " + assoc.getName() + " child=" + assoc.getChildAssetType() + "<br/>");
for (AssetId assocAsset : a.getAssociatedAssets(assoc.getName())) {
out.println(" found associated asset: " + assocAsset.getType() + ":" + assocAsset.getId() + "<br/>");
}
}
}
%>

Disclaimer: Any sample code on this blog is not officially recommended, use at your own risk.

Add assets to workflow programatically

Within WCS, Workflows are the way to initiate assets from initial creation step to publish step by going through series of steps & condition wherein each step is governed by different group of users with different permissions.

Sometimes when assets were initially created, they may or may not be in workflow. But sudden requirement like assets should be editable by only certain users, workflow can be best solution for it as WCS can restrict users from editing/deleting by checking against Roles.

But even if you define workflow, how to assign workflow to an asset?
Well, after creating workflow, you have to inspect the asset in Contributor UI -> From Top Navigation, click on View -> Status -> Select workflow process and the participants. These are quite a few steps to do assign workflow for many assets, thus, a requirement to have a utility wherein one could assign workflow to many assets at once.

Procedure:
1. Find the condition by which you can filter which assets are required to be in workflow. For e.g. only page assets are required to be in workflow, only content assets of particular type, etc. Once decided, just write a query + condition using Asset API
2. Loop through each assets and while in loop assign asset to workflow as shown:
<%
String pubid = ics.GetSSVar("pubid");
String workflowid = "[Your workflow process id]";
//Loop through asset list
IList contentList = ics.GetList("contentList",false);
if(null != contentList && contentList.hasData()){
int numRows = contentList.numRows();
for(int i=1; i<=numRows; i++){
contentList.moveTo(i);
String id = contentList.getValue("contentid"); //asset id
%><%-- Load the asset in workflow --%>
<workflowasset:load objvarname="workflowasset" assettype="<%= assetType %>" id="<%= id %>" />
<%-- Check if the asset is already in workflow or not --%>
<workflowengine:isobjectassigned object="workflowasset" varname="isAssetAssigned"/><%
String isAssetAssigned = ics.GetVar("isAssetAssigned");
if(Utilities.goodString(isAssetAssigned) && Boolean.valueOf(isAssetAssigned)){
%>Skipping this asset: <%= id%> as it is already in workflow.<br><%
ics.RemoveVar("isAssetAssigned");
} else {
%><%-- Get potential participants for workflow step 1 --%>
<workflowengine:getpotentialparticipants site="<%= pubid %>" objvarname="participants" process="<%= workflowid %>" object="workflowasset"/>
<%-- Set the asset in workflow --%>
<workflowengine:startobjectworkflowprocess site="<%= pubid %>" object="workflowasset" id="<%= workflowid %>" participants="participants"/><%
if(ics.GetErrno() == 0){
%>Successfully added asset: <%= id%> to workflow<br><%
} else {
%>Failed to add asset: <%= id%> to workflow<br><%
}
}
}
ics.RegisterList("contentList",null);
}
%>

Disclaimer: Any sample code on this blog is not officially recommended, use at your own risk.

A simple code compare functionality

One of the most important aspect of any development cycle is deployment and while deployment, it is very important to note the changes don...