Wednesday, December 2, 2009

How to access Alfresco repository in Alfresco Share

This Blog entry will provide you an example of Web Script to access Alfreso repository into Share

A Web Script is simply a service bound to a URI which responds to HTTP methods such as GET, POST, PUT and DELETE.

To know, in detail about Web Script, please go through, http://wiki.alfresco.com/wiki/Web_Scripts

Lets walk through an example, I am going to show you, how you can access Alfresco data repository into Alfresco Share.
Just take a scenario, Alfresco Share doesn’t provide you workflow history list which is available in Alfresco explorer or web client. Now, you might get requirement to get this history in Share by dashlet.

Here, I am going to show you just simple example – Document Property Web Script.

As, you might know, to In Share, you cant directly access Alfresco repository, it can be done either by RESTful API Reference or third party data fetched from XML or JSON sources based on the standard mechanisms provided by the Surf Platform.

Here, Document Property Web Script will simple output some document’s property like name, date of creation etc. in JSON format in Alfresco and I would retrieve this text in Alfresco Share.

PART 1
So first lets create a web script in alfresco for that you need following files.
docprop.get.desc.xml
docprop.get.json.ftl


Here, we don’t need .js file, as we are not going to have any specific behavior for our web script.

Create all above files in your alfresco web client’s Company Home( Data Dictionary ( Web Scripts ( Org ( Alfresco ( sample.

docprop.get.desc.xml


Enter the following code in your description file

<webscript>
<shortname>Document Property</shortname>
<description>It provides document’s name and date of creation and Creator property</description>
<url>/sample/docprop.json</url>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

For an example, I have created Space  TestFolder in CompanyHome and TestFolder contains  test.txt, readmeWebScripts.txt files and one space named Data .

docprop.get.json.ftl
Enter the following code in json.ftl file, it would retrieve children of space TestFolder and its property in Json format.

{

"docprop" : [
<#list companyhome.childByNamePath["TestFolder"].children as child>
{
    "name" : "${child.properties.name}" ,
  
    "creator" : "${child.properties.creator}",

   "CreatedDate" : "${child.properties.created?datetime}"
}
<#if child_has_next> , </#if>

</#list>
]
}


now, try your webscript
goto ,,   http://localhost:8080/alfresco/service/index
Click on Refresh Web Scripts

If everything is fine then it will give you following message with number of web scripts that obviously may differ.

Maintenance Completed
Reset Web Scripts Registry; registered 229 Web Scripts. Previously, there were 228.

Check the result of the web script by following URI as per our example

http: //<host>:<port>/alfresco/service/sample/docprop.json

In my case, it will be

http://localhost:8080/alfresco/service/sample/docprop.json

Output will be docprop.json file which would contain

{
"docprop" : [
{
    "name" : "test.txt" ,
     "creator" : "admin",
     "CreatedDate" : "Nov 16, 2009 10:35:37 AM"
}
 ,
{
    "name" : "readmeWebScripts.txt" ,
    "creator" : "admin",
   "CreatedDate" : "Nov 16, 2009 10:35:37 AM"
}
 ,
{
    "name" : "Data" ,
    "creator" : "admin",
     "CreatedDate" : "Nov 16, 2009 11:14:35 AM"
}
]}


PART 2

Here, we will create a dashlet and this dashlet would use docprop.json file to retrieve data.

To create a dashlet we require following three files.

docproperty.get.desc.xml
docproperty.get.js
docproperty.get.html.ftl


As per your installation folder

(C:\Alfresco\tomcat\webapps\share\WEB-INF\classes\alfresco\site-webscripts\org\alfresco\components\dashlets)

Create all three files in dashlet folder.

docproperty.get.desc.xml

Enter the following code in your description file

<webscript>
   <shortname>Documen Property</shortname>
   <description>Dashlet to list document property from Alfresco</description>
   <family>user-dashlet</family>
   <url>/components/dashlets/docproperty</url>
</webscript>

docproperty.get.js

Enter the following code in your .js file, which would be used to connect to Alfresco repository to retrieve data of docprop.json file


var connector = remote.connect("alfresco");
var data = connector.get("/sample/docprop.json");

// create json object from data
var result = eval('(' + data + ')');
model.docprop = result["docprop"];

docproperty.get.html.ftl

Enter the following code in your html.ftl file which would use this docprop object  to retrieve data

<table>
<tr>
    <th>Name </th>
    <th>Creator </th>
    <th>Date of Creation </th>
</tr>
<#list docprop as t>
<tr>
    <td>${t.name}</td>
    <td>${t.creator}</td>
    <td>${t.CreatedDate}</td>
</tr>
</#list>
</table>

To test the dashlet
Go to http://localhost:8080/share/service/index

Click on Refresh Web Scripts

If everything is fine then it will give you following message with number of web script.(which obviously may differ)

"Maintenance Completed
Reset Web Scripts Registry; registered 179 Web Scripts. Previously, there were 178 "

Login in to your Share (http://localhost:8080/share/)

Click on Customize the dashboard,  ( Add Dashlet,
Here you should find our new dashlet named - Document Property, Add it

And on your dashboard, you will find the property list, which we retrieved in Alfresco webscript.

14 comments:

  1. hi Niketha, Thanq very much, its helped so much to me, how long are you working on alfresco.

    ReplyDelete
  2. Wow, this should on the front page of alfresco's web script wiki :)

    I'm reading almost a whole working day now to get started with web scripts, this is a dummy guide my cat would understand!

    ReplyDelete
  3. Hi Niketha.. Excellent work..Could you please let me know is there is any way to add metadata using dashlet in Alfresco share?

    ReplyDelete
  4. Hi Niketa

    i am new to the Alfresco.
    i am using Alfresco 2.1 version can u provide sample webscripts and alfresco documentation.

    ReplyDelete
  5. Hi Niketa.. Thanks a Lot 2 thumbs up :)

    but, can you guide me to modify the webscript, when i just wanna show it in alfresco not in share??

    Best Regards

    ReplyDelete
  6. Hi Niketa,thank you very much :):):):):):)

    A question:

    How can we deploy a webscript on Alfresco Share(not alfresco)? if so,how can we do this?

    my email : dj_boy2010@yahoo.com

    ReplyDelete
  7. Excellent post, very helpful. Thanks!!!

    Michiel

    ReplyDelete
  8. After Click on Refresh Web Scripts i am getting the following error
    Maintenance Completed
    Reset Web Scripts Registry; registered 451 Web Scripts. Previously, there were 451.
    Warning: found 1 broken Web Scripts. Previously, there were 1.

    Broken Web Scripts
    02090004 Failed to parse web script description document org/alfresco/sample/docprop.get.desc.xml ; Error on line 3 of document : Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence. (workspace://SpacesStore/app:company_home/app:dictionary/cm:webscripts/org/alfresco/sample/docprop.get.desc.xml)

    ReplyDelete
    Replies
    1. Hi Vinay,

      I am same issue during access Alfresco Repository in Alfresco Share.

      After Click on Refresh Web Scripts i am getting the following error
      Maintenance Completed
      Reset Web Scripts Registry; registered 455 Web Scripts. Previously, there were 455.
      Warning: found 1 broken Web Scripts. Previously, there were 1.

      Broken Web Scripts
      10280007 Failed to parse web script description document org/alfresco/sample/docprop.get.desc.xml ; Error on line 3 of document : Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence. (workspace://SpacesStore/app:company_home/app:dictionary/cm:webscripts/org/alfresco/sample/docprop.get.desc.xml)

      Any help?

      Thanks,
      Mukesh

      Delete
  9. I clear the error just a small mistake while writing a .

    ReplyDelete
  10. Hello,

    do you know how it would be possible to display content in a dashlet ?
    Javascript seems to have a problem with it and with the object content.

    ReplyDelete
  11. I'm getting an error on a line in the Share js:

    05030007 Failed to execute script 'classpath*:alfresco/site-webscripts/org/alfresco/components/dashlets/docprop.get.js': 05030006 TypeError: Cannot read property "docprop" from null (file:/E:/Alfresco/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/dashlets/docprop.get.js#6)

    Apparently model is null. Where should I instantiate model? Or is there something automagically available?

    ReplyDelete