06-30-2020 07:36 AM
Hii All,
I want to create Site through webscript and i have created one but it doesnt work correctly.Hereby i am attaching code please do help out.
package com.abc.repo.web; import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.search.SearchService; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceServiceMemoryImpl; import org.alfresco.service.namespace.QName; import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteMemberInfo; import org.alfresco.service.cmr.site.SiteService; import org.alfresco.service.cmr.site.SiteVisibility; import org.springframework.extensions.surf.util.ParameterCheck; import org.springframework.extensions.webscripts.AbstractWebScript; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class siteCreation extends AbstractWebScript { private static Logger LOGGER = Logger.getLogger(siteCreation.class); private SiteInfo siteInfo; private SiteService siteService; private ServiceRegistry serviceRegistry; public void setServiceRegistry(ServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; } public void setSiteService(SiteService siteService) { this.siteService = siteService; } @Override public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException { JSONObject reqData = (JSONObject) req.parseContent(); String shortName, visibility, sitePreset, title, description = null; try { shortName= (String) reqData.get("shortName"); sitePreset= (String) reqData.get("sitePreset"); title= (String) reqData.get("title"); description= (String) reqData.get("description"); visibility= (String) reqData.get("visibility"); } catch (JSONException err) { err.printStackTrace(); } } public SiteInfo createSite(String sitePreset, String shortName, String title, String description, String visibility) { ParameterCheck.mandatoryString("visibility", visibility); SiteVisibility siteVisibility = SiteVisibility.valueOf(visibility); SiteInfo siteInfo = this.siteService.createSite(sitePreset, shortName, title, description, siteVisibility); return new SiteInfo(siteInfo, this.serviceRegistry, this.siteService); } }
07-06-2020 09:21 AM
@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
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.
07-07-2020 10:39 AM
I did not see this error of yours @piyush48
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.
Explore our Alfresco products with the links below. Use labels to filter content by product module.