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.

package com.test.alfresco.share;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONException;
import org.json.JSONObject;
public class CreateShateSite {
public static void main(String[] args) throws HttpException, IOException, InterruptedException {
HttpClient client = new HttpClient();
String loginData = "username=admin&password=admin"; // provide username and pwd
PostMethod loginPost = new PostMethod("http://127.0.0.1:8080/share/page/dologin");
loginPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
loginPost.setRequestEntity(new StringRequestEntity(loginData, "text/plain", "UTF-8"));
// make a post call to get JSESSIONID and other required cookies
System.out.println("dologin script status: " + client.executeMethod(loginPost));
// Make a share touch call to initiate/validate session
GetMethod authenticationGet = new GetMethod("http://127.0.0.1:8080/share/service/modules/authenticated?a=user");
System.out.println("User is authenticated script status : " + client.executeMethod(authenticationGet));
//call share script to create a site.
PostMethod createSitePost = new PostMethod("http://127.0.0.1:8080/share/service/modules/create-site");
String shortName = "TestSiteFromCode";
JSONObject siteObject = new JSONObject();
try {
siteObject.put("shortName", shortName);
siteObject.put("Visiblity", "Public");
siteObject.put("sitePreset", "site-dashboard");
siteObject.put("title", shortName);
siteObject.put("description", shortName);
createSitePost.setRequestHeader("Content-Type", "application/json");
createSitePost.setRequestHeader("Accept", "application/json");
createSitePost.setRequestEntity(new StringRequestEntity(siteObject.toString(), "application/json", "UTF-8"));
int status = client.executeMethod(createSitePost);
System.out.println("create a site script status :: " + status);
if (status == HttpStatus.SC_OK) {
System.out.println("Site created OK");
}
else{
System.out.println("There is error in site creation");
}
} catch (JSONException err) {
err.printStackTrace();
}
}
}
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/

<config evaluator="string-compare" condition="CSRFPolicy" replace="true">
<filter>
<rule>
<request>
<method>POST</method>
<path>/service/modules/create-site</path>
</request>
<action name="assertReferer">
<param name="always">false</param>
</action>
<action name="assertOrigin">
<param name="always">false</param>
</action>
</rule>
</filter>
</config>

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