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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |