Usually when you develop some new features for Alfresco quite often you need to completely clean the project and purge database. Which is not so good since you will loose all manually created things such us folders, users or sites. With sites it could be quite complicate to manually recreate them. In this post I’ll to show how to create sites automatically using webscript. So instead of creating sites one by one, manually insert names and privacy settings you’ll just need to call a webscript. And also with this method you could be sure that all your sites are in place.
I found few tutorials on how to do this, but some of them were too complicated, some just didn’t work. First thing which probably could do the work is siteService
in repo project. But the problem with it that it only creates site on repo side. So share wouldn’t know anything about it. Another approach which worked for me is to use create-site
webscript. It is a POST webscript, so you’ll need to pass all the site parameters to it.
Create a webscript description:
Implement the webscript.
CSRF Policy
In the latest versions of Alfresco (4+ versions) developers introduced CSRF Policy, more details here: Introducing the CSRFPolicy in Alfresco Share. For this site creation method it means that you’ll need to pass one more parameter in post request header which you can get from HTTPClient
after login.
Please also notice that token you get is encoded, so you need to decode it:
Done! So now you’ll just need to run the webscript by going to http://localhost:8080/alfresco/service/org/init/create-sites and your site is created.
Some problems
For the moment there is one problem with this method which I still cannot resolve. For some reason the first post request of site creation is ignored. So in my example only one site will be created.
Working project you can find on my github.