Creating Site using Webscript

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member II

Creating Site using Webscript

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.aviva.repo.webscripts;


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);
    }
}
21 Replies
Highlighted
Community Manager
Community Manager

Re: Creating Site using Webscript

Hi @piyush48,

There's a StackOverflow answer that you might find useful. 

HTH,

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Highlighted
Active Member II

Re: Creating Site using Webscript

Hi @EddieMay ,

 

I have referred to that and created my site but in answer ,the site is created by javascript function and we are using abstractWebscript to create the site. Could you please help me out how could i create it using AbstractWebscript only.

 

Thanks,

Piyush

Highlighted
Community Manager
Community Manager

Re: Creating Site using Webscript

Hi @piyush48,

In the StackOverflow answer, it states that you can't use a WebScript as

 

Creation of sites in Alfresco is done by invoking a Share module

Could you look into using the ReST API - http://localhost/api-explorer/#!/sites/createSite?

 

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Highlighted
Moderator
Moderator

Re: Creating Site using Webscript

@piyush48  There ways to create site. @EddieMay provided a link that has thread on creating site, which intern calls repository apis and does the job. You can create surf webscript to create site.

You can also create java or js webscript on repository side to create the site like you are doing. 

The code you have written is somewhat correct but there are several issues with your code including a compilation error. You are trying to instantiate an Interface named "SiteInfo". Interface can not be directly instantiated. It is always good to try and debug and check the errors. 

This return statement is wrong:

return new SiteInfo(siteInfo, this.serviceRegistry, this.siteService);

when you call below method:

this.siteService.createSite(sitePreset, shortName, title, description, siteVisibility);

It returns the SiteInfo type of object. You can simply return it from your method. Example:

public SiteInfo createSite(String sitePreset, String shortName, String title, String description,
			String visibility) {
   ParameterCheck.mandatoryString("visibility", visibility);
   SiteVisibility siteVisibility = SiteVisibility.valueOf(visibility);
return this.siteService.createSite(sitePreset, shortName, title, description, siteVisibility); }

Refer:
https://github.com/Alfresco/alfresco-repository/blob/master/src/main/java/org/alfresco/service/cmr/s...
https://github.com/Alfresco/alfresco-repository/blob/master/src/main/java/org/alfresco/service/cmr/s...
https://dev.alfresco.com/resource/docs/java/org/alfresco/service/cmr/site/SiteService.html#createSit...

I also don't see where you are calling "createSite" method in your execute(WebScriptRequest, WebScriptResponse) method?

 

Also these declarations are unnecessary:

private SiteInfo siteInfo;
private ServiceRegistry serviceRegistry;

public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
  this.serviceRegistry = serviceRegistry;
}

You are just using SiteService, so SiteInfo  and ServiceRegistry are not required in this specific webscript. 

Class naming is not correct :

 Wrong class naming: 

siteCreation 

Correct class naming:

SiteCreation 

 

Highlighted
Active Member II

Re: Creating Site using Webscript

Thanks @abhinavmishra14  and @EddieMay ,

I have created surf Webscript as @abhinavmishra14  helped me by guideing me to correct code implementation.

Site can be now created by webscript but it doesnt show document library and site-dashboard and when i click on customize site page to add this component it is showing error of component-pages.get.js is failed to get executed. Please do help me out by guiding me towards solution.

I am attaching photo of error which i am getting.

 

Thanks,

PiyushScreenshot (36).png

Highlighted
Community Manager
Community Manager

Re: Creating Site using Webscript

Hi @piyush48,

Can you share the code from your webscript please?

Thanks, 

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Highlighted
Active Member II

Re: Creating Site using Webscript

Hii @EddieMay  and @abhinavmishra14 ,

please find the attached code for the Java Webscript for creating site as suggested by abhinav.

package com.aviva.repo.webscripts;

 

import java.io.IOException;

 

import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.cmr.site.SiteVisibility;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;

 

public class CreateSite extends AbstractWebScript {

 

    private SiteService siteService;

 

    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 = "";
       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");
                createSite(sitePreset,shortName,title,description,visibility);
            }
            catch (JSONException err) {
                err.printStackTrace();
            }
            System.out.println("Site with "+ shortName +"is needed to be created");
     }

 

    public void createSite(String sitePreset, String shortName, String title, String description, String visibility)
    {
        System.out.println("data------------------  " + this.siteService.getSite(shortName));

 

        if(this.siteService.getSite(shortName) == null){
            SiteVisibility siteVisibility = SiteVisibility.valueOf(visibility);
            SiteInfo siteInfo = this.siteService.createSite(sitePreset, shortName, title, description, siteVisibility);
            System.out.println("siteinfo ------------------  " + siteInfo.getShortName());
        }else {
            System.out.println("Site is already exists");
        }

 

    }
}

Thanks,

Piyush

Highlighted
Community Manager
Community Manager

Re: Creating Site using Webscript

Hi @piyush48,

What are you passing in as sitePreset? 

If I create a site using the API

http://localhost:80/alfresco/api/-default-/public/alfresco/versions/1/sites?alf_ticket=TICKET_4ad80403182aac65b38d69efa88998814c6d80be

with the following values:

{
  "id": "publicSiteAPI",
  "title": "Public Site By API",
  "description": "Public site created for blog post",
  "visibility": "PUBLIC"
}

The return value I get is:

{
    "entry": {
        "role": "SiteManager",
        "visibility": "PUBLIC",
        "guid": "3ab2a35a-2c21-40cf-b14b-ae1eb42288ce",
        "description": "Public site created for blog post",
        "id": "publicSiteAPI",
        "preset": "site-dashboard",
        "title": "Public Site By API"
    }
}

created by APIcreated by API

I'm wondering if your "preset" is incorrect?

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Highlighted
Active Member II

Re: Creating Site using Webscript

Hii @EddieMay ,

Please find my JSON values which i pass to create site.

{
"sitePreset":"Aviva Dashboard",
"shortName":"Aviva-Test",
"title":"AvivaTestNew",
"description":"Demo Aviva Site",
"visibility":"PRIVATE"
}