Creating Site using Webscript

cancel
Showing results for 
Search instead for 
Did you mean: 
abhinavmishra14
Advanced

Re: Creating Site using Webscript

@piyush48 couple of things.

1- Can you check your json payload in CreateSite class, and see what values you are getting ?

Probably log the json payload. 

JSONObject reqData = (JSONObject) req.parseContent();

LOGGER.info("CreateSite reqData: "+reqData);

2- Can you please clarify whether you want to create Collaboration Site (which has default preset as 'site-dashboard'. It is out of the box), or you are looking for a specific type of site ? @EddieMay  provided default site preset as mentioned above which you would be using to create a Collaboration site.

Also, when a user creates the site, they have full permissions on site to customize etc. Can you also check whether you are using the same user to customize the site ?

i see that EddieMay and sanjay also provided the link to ootb rest api which takes input as:

(POST) http://<host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/sites?alf_ticket=TICKET_xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz

{
"id": "test",
"title": "Test",
"description": "This is a test site",
"visibility": "PRIVATE"
}

To create a site with default site preset i.e. site-dashboard. This should be sifficient to create sites as long as you don't have site specific requirement (i.e. creating a site with custom site preset). 

Additionally, you can also use this REST API:

(POST) http://<host>:<port>/alfresco/service/api/sites?alf_ticket=TICKET_xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz

And pass all params including site preset:

{
"shortName": "test",
"title": "Test",
"description": "This is a test site",
"visibility": "PRIVATE",
"sitePreset": "site-dashboard"
}

So if you have a custom site preset, you can simply change the value of "sitePreset".

{
"shortName": "test",
"title": "Test",
"description": "This is a test site",
"visibility": "PRIVATE",
"sitePreset": "custom-site-dashboard"
}

For creating cusotm presets have a look at this doc and example project:

https://docs.alfresco.com/6.2/tasks/dev-extensions-share-tutorials-js-customize.html

https://github.com/Alfresco/alfresco-sdk-samples/blob/alfresco-51/all-in-one/customize-webscript-con...

Invoking, "/alfresco/api/-default-/public/alfresco/versions/1/sites" or "/alfresco/service/api/sites" is same as you would invoke your custom CreateSite webscript. So a custom webscript may not be needed unless there is something very specific you want to do.

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
abhinavmishra14
Advanced

Re: Creating Site using Webscript

I did not see this error of yours @piyush48 

https://hub.alfresco.com/t5/alfresco-content-services-forum/creating-site-using-webscript/m-p/300057...

I can recall this error, this error is expected as siteService allows to create site but it doesn't setup surf config which is required for every site. This error comes when surf-config is missing. 

You can go to Repository > sites > select site node which is created using your script, And check it in node browser, you will see that surf-config association is missing. Where as ootb rest api does setup surf-config along with creating site, but does't allow custom presets. 

If you create surf webscript instead of repo webscript, you would get siteData root scoped object which allows setting up surf config. 

So question is whether you need default site with preset (site-dashboard) or you are going to use custom preset? 

This api is sufficient for default site as mentioned earlier: 

http://<host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/sites?alf_ticket=TICKET_xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz

 

For your reference here is an example of surf webscript in case you would like to use that. Deploy this under "/<yourshare-module>/src/main/resources/alfresco/web-extension/site-webscripts/*" or "<ALF_INSTALLATION>\tomcat\shared\classes\alfresco\web-extension\site-webscripts/*"

 

create-site.get.js

var siteCreationLog = [];

function main() {
	model.success = false;

	// Default preset is "site-dashboard"
	// Use this url to create site: 
	// e.g.: http://<hos>:<port>/share/page/demo/modules/create-site?sitePreset=whitepapers-site-dashboard&shortName=whitetest&title=WhiteTest
	
	var shortName = args.shortName;
	var sitePreset = args.sitePreset != null ? args.sitePreset : "site-dashboard";

	if (shortName != null) {
		shortName = shortName.replace(/[^0-9a-zA-Z\-\s]/g, "");
		shortName = shortName.trim();
		shortName = shortName.replace(/\s+/g, "-").toLowerCase();
		var title = args.title != null ? args.title : shortName;
		var visibility = args.visibility != null ? args.visibility : "PUBLIC";
		var description = args.description != null ? args.description : "";

		var jsonPayload = '{"title":"' + title + '","visibility":"' + visibility + '","description":"' + description + '","sitePreset":"'
				+ sitePreset + '","shortName":"' + shortName + '"}';
		
		var clientRequest = jsonPayload.toString();
		var clientJSON = JSON.parse(clientRequest);

		var remoteConnection = remote.connect("alfresco");
		var repoResponse = remoteConnection.post("/api/sites", clientRequest, "application/json");

		if (repoResponse.status == 400) {
			siteCreationLog.push("Site already exists!");
			model.siteCreationLog = siteCreationLog;
		} else if (repoResponse.status == 401) {
			siteCreationLog.push("Unable to create site. Please check the logs for error details!");
			model.siteCreationLog = siteCreationLog;
		} else {
			var jsonRespFrmRepo = JSON.parse(repoResponse);
			if (jsonRespFrmRepo.shortName) {
				for (var each = 0; each < 3 && !model.success; each++) {
					var tokens = [];
					tokens["siteid"] = jsonRespFrmRepo.shortName;
					//setup surf-config.
					model.success = sitedata.newPreset(clientJSON.sitePreset, tokens);
				}
				if (model.success) {
					siteCreationLog.push("Site created successfully.");
					model.siteCreationLog = siteCreationLog;
				} else {
					conn.del("/api/sites/" + encodeURIComponent(jsonRespFrmRepo.shortName));
					status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, "error.create");
				}
			} else if (jsonRespFrmRepo.status.code) {
				status.setCode(jsonRespFrmRepo.status.code, jsonRespFrmRepo.message);
			}
		}
	} else {
		siteCreationLog.push("Unable to create site.");
		model.siteCreationLog = siteCreationLog;
	}
}

main();

create-site.get.desc.xml:

<webscript>
	<shortname>Site Creation Script</shortname>
	<description>Utility to create site.</description>
	<url>/demo/modules/create-site</url>
	<transaction>required</transaction>
</webscript>

create-site.get.html.ftl

<#list siteCreationLog as log>
 ${log} <br/>
</#list>

 

 

 

If you still want to use java backed webscript and have site preset specific requirement then you would have to write logic to import the surf-config from within your java backed webscript. 

 

 

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)