Sunday, August 31, 2014

Display web-preview of document on form in Alfresco share



Environment:  Alfresco 4.2.1 Enterprise Version

Recently we had requirement to have web-preview of the content on work flow task form.
Actually, in out of the box web-preview is an independent component and form itself is an independent component.
On form we can have only different types of controls not the component.
So there is no direct way to display web-preview of the content on any form as we don't have direct facility to render component on form.

One possible solution is make a custom control and render the response of the web-preview component in custom control.

That is what exactly I did.

Let's have brief idea what we are going to do.

we will create one custom control for that we need to create one ftl and one client side java-script file.
In ftl we will just add div in which we will render web-preview component response.
From ftl file, we will call client side js file.
From onReady method of client side js, we will call web-preview component and will embed its response in div which is defined in ftl.

I created custom control and added the following code in ftl file.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<@script type="text/javascript" src="${page.url.context}/res/components/form/custom-web-preview.js"></@script>
   <@script type="text/javascript" src="${page.url.context}/res/components/preview/web-preview.js"></@script>
   <@script src="${url.context}/res/components/preview/web-preview.js" />
   <@script src="${url.context}/res/components/preview/WebPreviewer.js" />
   <@script src="${url.context}/res/js/flash/extMouseWheel.js" />
   <@script src="${url.context}/res/components/preview/StrobeMediaPlayback.js" />
   <@script src="${url.context}/res/components/preview/Video.js" />
   <@script src="${url.context}/res/components/preview/Audio.js" />
   <@script src="${url.context}/res/components/preview/Flash.js" />
   <@script src="${url.context}/res/components/preview/Image.js" />

 <script type="text/javascript">//<![CDATA[
(
function()
{
    new Alfresco.customControl("${fieldHtmlId}").setMessages(${messages});
}
)
();

//]]></script>

<div id="${fieldHtmlId}">
   <div id="web-preview">
     
 </div>
</div>

Here, I have added dependences java-script files and added Div with id as "web-preview" which will be used to render response of the web-preview component.

In onReady method of client js, I called ootb web-preview component and added its response in to defined "web-preview" div.
Following code I added in onReady method of client side java-script.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Alfresco.util.Ajax.request(
         {
           method: "GET",
           url: Alfresco.constants.URL_SERVICECONTEXT + "components/preview/web-preview?nodeRef=${NODEREF}&htmlid=${ID}",,
           successCallback:
           {
            fn: function(o)
            {
        Dom.get("web-preview").innerHTML = o.serverResponse.responseText;
            },
            scope: this
           },
           failureMessage: "Failed"
        });
         }
         

       
That's it we are done.Now we need to just add this custom control on any form and it will display web-preview of content on form UI.

Tuesday, May 20, 2014

Execute Alfresco RESTful APIs using "Advanced REST client "

     There are numerous of plugins are available in Google Chrome and FireFox browsers for doing rest calls like Poster,Rest-Console etc.

I found "Advanced REST client" is the best one as it is providing advanced facility like saving history and project but its user interface is pretty awful.I dont like that one  :)

For understanding How one can use "Advanced REST client"  in actual scenarios, I am taking example of doing post request with json data as body.

First we need to install "Advanced REST client" chrome extension in Google Chrome browser.
You can find it here https://chrome.google.com/webstore/search/advanced%20rest%20client .

After installing "Advanced REST client" chrome extension in Google chrome, go to chrome://apps/.

You can find the icon for "Advanced REST client" extension.Click on it.Its home will look like :






Now, enter the URL of your rest call in URL tab.

Select Post request method and add header parameters in "Header" panel.

By clicking on "Form" tab, you can add your credentials as header parameters.
You can also new header parameter by click on "Add new Header" link.

Write your raw json data in "Payload" panel.
By clicking on "Raw" tab, you can add your raw json data in payload.

As we are passing json data in body,we will  select "application/json" from drop down.
Now our page look like following :






Now click on "send" button, you will get response in response tab.
You can also save the request and in future you can use it.