Here I want to explain how to create a custom site page:
which points to any folder you want inside your repository. It will have kind of the same behaviour as My Files button. For example let’s pretend that each user has it’s own folder which name matches the username.
The idea is to copy a document library page and change some components to make them open a folder inside document library. To get the name of this folder we will use a Java-backed webscript which will search for it inside document library and return it’s nodeRef. It’s as simple as it sounds, but when you do it for the first time it seems quite hard, especially if you are not an Alfresco ninja. We can split the task in three simple steps:
- Create a custom page.
- Create a webscript which returns the NodeRef of the folder.
- Link page and webscript together.
By default inside any Alfresco site you have three visible pages: Dashboard, Document library, Site members. To create a new page you can use an Alfresco tutorial: Add a new page to Alfresco Share or follow these steps. Please don’t forget that it should be done in share project. Create following files:
- Page definition - /share-amp/src/main/amp/config/alfresco/web-extension/site-data/pages/my-document-library.xml
Template-Instance definition - /share-amp/src/main/amp/config/alfresco/web-extension/site-data/template-instances/my-document-library.xml. For the moment you can just copy it from
FreeMaker template - /share-amp/src/main/amp/config/alfresco/web-extension/site-data/template/my-document-library.ftl. Copy this one from
At the end the folder structure should look like this:
That’s it! The page is created, you can view it on http://localhost:8080/share/page/my-document-library.
Since we copied templates from documentlibrary it will look exactly the same as Document Library page. But with broken title, which would be something like
page.myDocLib.title, to fix it update the properties file:
But this page is not linked to your site yet. To link it add this part to
And then on site-customization page just drag and drop created page from Available Site Pages to Current Site Pages.
First step is done!
Create Java-Backed WebScript
Please have a look Alfresco-wiki page for more information about webscripts.
This WebScript will be called by created page and return the nodeRef of the folder which name is the same as the username of logged user.
First we need to create a webscript description file: /repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/getUserNodeRefWS.get.desc.xml with following content:
Java. Create GetUserNodeRefWS.java class under /repo-amp/src/main/java/org/alfresco/webscript/. The whole class you can find on gitHub, here I’ll just write the main method which does all the work:
And now let’s add some Spring magic which will link your class with the webscript. In
service-context.xml add a new bean:
Now you can test the webscript, just go to http://localhost:8080/alfresco/s/api/mywebscript/getnoderef?param=admin and you should have a response with a nodeRef. Don’t forget to create the folders inside site’s document library otherwise you’ll have an exception.
Link Page and webscript together
To link them we need to rewrite myfiles component. This component will call a webscript and show the folder with nodeRef which webscript returned.
Copy following files:
to the site-webscripts/components/myDocumentLibrary folder and rename them to
Now let’s change them a bit.
- myDocumentLibrary.get.desc.xml - url refers to the component name:
- myDocumentLibrary.get.html.ftl - modify imports:
- myDocumentLibrary.get.js - add a method which will call a webscript:
And pass it’s value to the rootNode variable in the beginning of the file, which then pass to the docListToolbar and documentList constructors:
The last step is to modify the widget call so that it will call a new one. In the template-instance created on the first step change the documentlist_v2 component:
And that’s it! The whole project you can find on the github.