How to get noderef in a custom dialog template

cancel
Showing results for 
Search instead for 
Did you mean: 
hiten_rastogi1
Established Member

How to get noderef in a custom dialog template

Hi,

I have created a custom dialog freemarker template for my custom field that I am using as itemkind in my custom action. The custom field will be auto populated with some values specific to a node and shown as a dropdown for user to choose a value. The custom dialog freemarker template for this field will make a call to share side webscript which in turn will call a repo side webscript to fetch the values. 

The issue I am facing is that I am not able to get the noderef in my custom dialog template. Can anyone please help me in knowing how I can get the noderef of the node on which the action is being executed ??

Below is my custom dialog template code.

<code>

<#include "/org/alfresco/components/form/controls/common/utils.inc.ftl" />

<script type="text/javascript">//<![CDATA[
(function()
{

var updateOptions = function(res){
var result = eval('(' + res.serverResponse.responseText + ')');
if(result.Options.length > 0 ) { // Options - returned JSON object
// do something with JSON data
}
}

Alfresco.util.Ajax.jsonGet({

// call the share side webscript
url : Alfresco.constants.URL_SERVICECONTEXT + "/custom/versions?noderef=" + file.nodeRef, // on the console I can see an exception which says that file is not defined.
successCallback : {
fn : function() {},
scope : this
},
failureCallback : {
fn : function() {},
scope : this
}
});


})();
//]]></script>

<#if field.control.params.optionSeparator??>
<#assign optionSeparator=field.control.params.optionSeparator>
<#else>
<#assign optionSeparator=",">
</#if>
<#if field.control.params.labelSeparator??>
<#assign labelSeparator=field.control.params.labelSeparator>
<#else>
<#assign labelSeparator="|">
</#if>

<#assign fieldValue=field.value>

<#if fieldValue?string == "" && field.control.params.defaultValueContextProperty??>
<#if context.properties[field.control.params.defaultValueContextProperty]??>
<#assign fieldValue = context.properties[field.control.params.defaultValueContextProperty]>
<#elseif args[field.control.params.defaultValueContextProperty]??>
<#assign fieldValue = args[field.control.params.defaultValueContextProperty]>
</#if>
</#if>

<div class="form-field">
<#if form.mode == "view">
<div class="viewmode-field">
<#if field.mandatory && !(fieldValue?is_number) && fieldValue?string == "">
<span class="incomplete-warning"><img src="${url.context}/res/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>
</#if>
<span class="viewmode-label">${field.label?html}:</span>
<#if fieldValue?string == "">
<#assign valueToShow=msg("form.control.novalue")>
<#else>
<#assign valueToShow=fieldValue>
<#if field.control.params.options?? && field.control.params.options != "">
<#list field.control.params.options?split(optionSeparator) as nameValue>
<#if nameValue?index_of(labelSeparator) == -1>
<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)>
<#assign valueToShow=nameValue>
<#break>
</#if>
<#else>
<#assign choice=nameValue?split(labelSeparator)>
<#if choice[0] == fieldValue?string || (fieldValue?is_number && fieldValue?c == choice[0])>
<#assign valueToShow=msgValue(choice[1])>
<#break>
</#if>
</#if>
</#list>
</#if>
</#if>
<span class="viewmode-value">${valueToShow?html}</span>
</div>
<#else>
<label for="${fieldHtmlId}">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>
<#if field.control.params.options?? && field.control.params.options != "">
<select id="${fieldHtmlId}" name="${field.name}" tabindex="0"
<#if field.description??>title="${field.description}"</#if>
<#if field.control.params.size??>size="${field.control.params.size}"</#if>
<#if field.control.params.styleClass??>class="${field.control.params.styleClass}"</#if>
<#if field.control.params.style??>style="${field.control.params.style}"</#if>
<#if field.disabled && !(field.control.params.forceEditable?? && field.control.params.forceEditable == "true")>disabled="true"</#if>>

// versions will be set by the share side webscript call in the script tag
<#list versions as version>
<option value="${version.label?html}"<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)> selected="selected"</#if>>${version.label?html}</option>
</#list>
</select>
<@formLib.renderFieldHelp field=field />
<#else>
<div id="${fieldHtmlId}" class="missing-options">${msg("form.control.selectone.missing-options")}</div>
</#if>
</#if>
</div>

</code>

3 Replies
afaust
Master

Re: How to get noderef in a custom dialog template

Sure - "file" is not defined because you have not provided it anywhere to the client-side JavaScript function and there is no magic "file" in the context of the form control rendering request. If this is an edit/view form, then you will have the "form.arguments.itemId" variable in the FTL context, which might be a NodeRef (if the form is rendered for one).

hiten_rastogi1
Established Member

Re: How to get noderef in a custom dialog template

Hi Alex,

When I use form.arguments.itemId to get the value of nodeRef I get version-delete as the value which is the itemId in my custom action. Below is the form that I am using and the corresponding action that invoke that form.

FORM

<blockcode>

<config evaluator="string-compare" condition="version-delete"> <!-- ID for the Repository Action that this form is associated with -->
<forms>
<form>
<field-visibility>
<show id="version"/>
</field-visibility>
<appearance>
<field id="version" label-id="action.versionDelete.form.field.version">
<control template="/form-controls/version-dropdown.ftl"  />
</field>
</appearance>
</form>
</forms>
</config>

</blockcode>

ACTION

<code>

<action id="action.versionDelete" type="javascript" label="action.versionDelete.label">
<param name="function">onActionFormDialog</param>
<param name="itemKind">action</param>
<param name="itemId">version-delete</param> <!-- Repository action id = Spring Bean id -->
<param name="mode">create</param>
<param name="destination">{node.nodeRef}</param>
<param name="successMessage">action.versionDelete.msg.success</param>
<param name="failureMessage">action.versionDelete.msg.failure</param>

</action>

</code>

hiten_rastogi1
Established Member

Re: How to get noderef in a custom dialog template

I was finally able to get the nodeRef in my custom ftl which was used in a field of a form which was invoked by an action. It seems if you define <param name="destination">{node.nodeRef}</param> in your action alfresco includes the noderef as a hidden input field with id as "alf-id24-form-destination" where alf-id24 is a part of fieldHtmlId e.g in my case fieldHtmlId was coming as alf-id24_prop_version. You can do document.getElementById("alf-id24-form-destination").value or YAHOO.util.Dom.get("alf-id24-form-destination").value to get the nodeRef. Full code of ftl below

<code>

<#include "/org/alfresco/components/form/controls/common/utils.inc.ftl" />

<script type="text/javascript">//<![CDATA[


YAHOO.util.Event.onContentReady("${fieldHtmlId}", function ()
{
var fieldHtmlIdToken = "${fieldHtmlId}".split("_");
var nodeRef = document.getElementById(fieldHtmlIdToken[0]+"-form-destination").value;
//var nodeRef = YAHOO.util.Dom.get(fieldHtmlIdToken[0]+"-form-destination").value;

Alfresco.util.Ajax.jsonGet({
url: encodeURI(Alfresco.constants.PROXY_URI + 'api/version?nodeRef='+nodeRef),
successCallback:
{
fn: function loadWebscript_successCallback(response, config)
{
var obj = JSON.parse(response.serverResponse.responseText);
if (obj)
{
for (i = 0; i < obj.length; i++) {

// avoid the latest version from getting displayed
if(i===0){
continue;
}
var newOption = document.createElement('option');
newOption.value = obj[i].label;
newOption.text = obj[i].label;
YAHOO.util.Dom.get("${fieldHtmlId}").options.add(newOption);
}
// Current value
var sp = document.getElementById("${fieldHtmlId}");
sp.value = "${field.value}";
}
}
}
});
}, this);
//]]></script>

<#if field.control.params.optionSeparator??>
<#assign optionSeparator=field.control.params.optionSeparator>
<#else>
<#assign optionSeparator=",">
</#if>
<#if field.control.params.labelSeparator??>
<#assign labelSeparator=field.control.params.labelSeparator>
<#else>
<#assign labelSeparator="|">
</#if>

<#assign fieldValue=field.value>

<#if fieldValue?string == "" && field.control.params.defaultValueContextProperty??>
<#if context.properties[field.control.params.defaultValueContextProperty]??>
<#assign fieldValue = context.properties[field.control.params.defaultValueContextProperty]>
<#elseif args[field.control.params.defaultValueContextProperty]??>
<#assign fieldValue = args[field.control.params.defaultValueContextProperty]>
</#if>
</#if>

<div class="form-field">
<#if form.mode == "view">
<div class="viewmode-field">
<#if field.mandatory && !(fieldValue?is_number) && fieldValue?string == "">
<span class="incomplete-warning"><img src="${url.context}/res/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>
</#if>
<span class="viewmode-label">${field.label?html}:</span>
<#if fieldValue?string == "">
<#assign valueToShow=msg("form.control.novalue")>
<#else>
<#assign valueToShow=fieldValue>
<#if field.control.params.options?? && field.control.params.options != "">
<#list field.control.params.options?split(optionSeparator) as nameValue>
<#if nameValue?index_of(labelSeparator) == -1>
<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)>
<#assign valueToShow=nameValue>
<#break>
</#if>
<#else>
<#assign choice=nameValue?split(labelSeparator)>
<#if choice[0] == fieldValue?string || (fieldValue?is_number && fieldValue?c == choice[0])>
<#assign valueToShow=msgValue(choice[1])>
<#break>
</#if>
</#if>
</#list>
</#if>
</#if>
<span class="viewmode-value">${valueToShow?html}</span>
</div>
<#else>
<label for="${fieldHtmlId}">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>
<select id="${fieldHtmlId}" name="${field.name}" tabindex="0"
<#if field.description??>title="${field.description}"</#if>
<#if field.control.params.size??>size="${field.control.params.size}"</#if>
<#if field.control.params.styleClass??>class="${field.control.params.styleClass}"</#if>
<#if field.control.params.style??>style="${field.control.params.style}"</#if>
<#if field.disabled && !(field.control.params.forceEditable?? && field.control.params.forceEditable == "true")>disabled="true"</#if>>
</select>
<@formLib.renderFieldHelp field=field />
</#if>
</div>

</code>