Thursday, April 7, 2016

Create Alfresco Share sites through Webscript.


I see many has requirement to create share site programmatically and OOB API is not available to create fully functional site.

Create a site is two step functionality.
     1. Create repository level site folder structure
     2. Create surf site presets objects.
So we need to call surf web script – “/service/modules/create-site”

As per Alfresco - If you call a Surf Web Script URL directly in a browser address bar there is no authentication (i.e. no context). You would need to have a JSESSIONID that is already authenticated (i.e. by the page). Surf ties the given JSESSIONID to the Alfresco TICKET that is stored in the session for that user for that connector (i.e. alfresco, alfresco-api).

I tried some posts to create site through script but it was not working for me. All scripts like dologin, create-site were getting executed OK with 200 status and without any error but when I check actual site it was not getting created.

Then I tried to login in to Share through browser and I used same cookies from browser to in my code then site was getting created perfectly OK so might be session was not getting initiated.
So I called authenticated service in between and its working fine.

 Below is working JAVA code.

NOTE: I have default Alfresco NTLM authentication and I have disabled CSRF token filter. I have verified it against Alfresco 5.x EE.

If You have not disabled CSRF token filter then add below snippet in your share-config-custom.xml located at [tomcat\shared\classes\alfresco\web-extension] as per - https://www.alfresco.com/blogs/ewinlof/2013/03/11/introducing-the-new-csrf-filter-in-alfresco-share/

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. There is an error in the code :

    siteObject.put("Visiblity", "Public");

    must be

    siteObject.put("visibility", "Public");

    you don't see the difference cause public is visibility by default.
    But if you want put moderated or private visibility it didn't work.
    With this correction, it's OK.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. is it possible to create site with python script?
    i have created site with python code.
    site is created but dashboard is not showing.

    ReplyDelete
  5. This works perefectly, thanks :)

    ReplyDelete
  6. how to do Alfresco NTLM authentication?

    ReplyDelete