cancel
Showing results for 
Search instead for 
Did you mean: 

Cound not Create Site in Alfresco Share programmatically

sselvan
Champ in-the-making
Champ in-the-making
I am trying to create a Site in Alfresco Share, programmatically.

      SiteInfo siteInfo = this.serviceRegistry.getSiteService().createSite("TestSite", "1234567", "for-santhana", "description", true);
      System.out.println("successful site, and the details are "+" preset= "+siteInfo.getSitePreset()+" shortname= "+siteInfo.getShortName());

The problem is - the site is getting created in Alfresco under Sites directory, but when I open the same in Alfresco Share, it does not open with dashboard, document library, wiki, blog etc.,

If you have any clues, please let me know. This will be a great help!!
16 REPLIES 16

mickael_istria
Champ in-the-making
Champ in-the-making
Hello,

I am also trying to create a Share site programmatically, using a JavaScript webscript.
But the blocking point is that I do not succeed to create a file in the AVM store programmatically. Here is a sample of code I am trying to get working:

   var components = avm.lookupNode("sitestore:/alfresco/site-data/components");
   if (components == null) {
      logger.log ('components null');
      return 507;
   }
   logger.log ('got store and components');
   
   var dashFile = components.createFile("page.title.site~" + shortName + "~dashboard.xml");
   logger.log ('got dash file');

With this script, everything works until the createFile (I get the got store and components displayed, then nothing but a 500 return code to client).

Can anyone help me to figure out why this does not work, or (better Smiley Wink ), how I can get it working ?

I already know that I can get it working for sure with Java, but I'd really prefer use JavaScript, to make deployment much easier.
Thanks in advance for your support.
Mickael

yqu
Champ in-the-making
Champ in-the-making
Let us first get back to all necessary steps for creating a site programmatically

1) On DM repo side, a node needs to be created under /Sites space and it needs to have all required metadata such as sitePreset etc. This will be taken care of by siteService.createSite. This API is available on DM site as both JAVA API and JavaScript API.  Keep in mind, it doesn't create any required data on AVM side.

2) on AVM repo side, for each site, we need to create all necessary SURF XML files such as dashboard.xml etc. All needed APIs/services for creating those files are available on Share side. It should be doable on DM site as well since we can use AVM related service to create files. But it will require much more work.

Now let us figure out possible solutions:

If we take a close look at the Share side create site service, you can see the extra step is

      // Check if we got a positive result
      if (repoJSON.shortName)
      {
         // Yes we did, now create the site in the webtier
         var tokens = new Array();
         tokens["siteid"] = repoJSON.shortName;
         sitedata.newPreset(clientJSON.sitePreset, tokens);

         model.success = true;
      }

The key is to populate the sitedata object once DM side siteService.createSite is invoked.

To validate it, let us first create a site on DM side

var site = siteService.createSite("site-dashboard", "yongtest", "YongTestSite", "YongTestSite", true);

The new site will be created. But if you click on the site link, it will be forwarded to user dashboard.

Now let us add a new webscript to share

File: custom-site.get.desc.xml

<webscript>
   <shortname>Utility Service for creating site programmatically</shortname>
   <description>Utility Service for creating site programmatically</description>
   <url>/modules/custom-site</url>
</webscript>

File:custom-site.get.html.ftl
empty

File: custom-site.get.js

var tokens = new Array();
tokens["siteid"] = args.shortName;
sitedata.newPreset(args.sitePreset, tokens);

Add all files to

share\WEB-INF\classes\alfresco\site-webscripts\org\alfresco \modules

Refresh Share webscript list.

Open a browser and hit the following link

http://localhost:8080/share/service/modules/custom-site ?shortName=yongtest&sitePreset=site-dashboard

Now, let us visit the site dashboard again and you you will find out it works.

So the conclusion is

The suggested solution is to write java code to invoke Share site service directly.

We can prepare a JSON object and post to the Share side service endpoint

/modules/create-site

Sample code (for how to prepare a JSON object and do a POST) can be found org.alfresco.repo.web.scripts.site.SiteServiceTest.

Again, we need to post it to the Share service endpoint not the DM service endpoint.

Yong

csrajuse
Champ in-the-making
Champ in-the-making
I was able to create a share site using Yong's post. The only difference being I user UrlConnection and CookieManager class (I wrote instead of creating a JSON object).

However the behavior was somewhat different from what I expected. Not sure if that is how it is supposed to be (my scanty Alfresco knowledge I guess?!)

When creating a site and calling the required url using post (setting the cookie.. all the fancy stuff) and was expecting that the default container like "documentLibrary" will be created and was grabbing the NodeRef for it. Unfortunately I discovered it is not creating immediately, but is being created only after someone was physically going to the site and clicking on the site to open from the browser.  Which does not serve my purpose.

So I created documentLibrary as a container in newly created share (in my infamous java code) and went ahead and had no issues (You may ask so what is your issue, I am just pointing it out so others do not waste time like I did)… Let me know if anyone wants code I can post it here…

mikeh
Star Contributor
Star Contributor
…was expecting that the default container like "documentLibrary" will be created and was grabbing the NodeRef for it. Unfortunately I discovered it is not creating immediately, but is being created only after someone was physically going to the site and clicking on the site to open from the browser.  Which does not serve my purpose.

So I created documentLibrary as a container in newly created share (in my infamous java code) and went ahead and had no issues (You may ask so what is your issue, I am just pointing it out so others do not waste time like I did)… Let me know if anyone wants code I can post it here…
That's correct: containers are not created until first accessed. This is something we may revisit in a future version, so you should always check for the container first before creating it - and always go through the Site Service's createContainer() to do so.

Mike

csrajuse
Champ in-the-making
Champ in-the-making
Thanks Mike,

For now I have wrapped that in an try catch block, so just in case if there is a change in the future release that piece of code should still work! of course I am assuming that an appropriate exception will be thrown by alfresco code.

lenap
Champ in-the-making
Champ in-the-making
Hello,
is there something new about this?
For my part I try to make a perl script to create share site.
Here's my approach, I use first POST /alfresco/service/api/ on the DM side:

my $GetRequest = HTTP::Request->new
      (
         'POST',
         "http://HOST:PORT/alfresco/service/api/sites?alf_ticket=$alfTicket",
         $h,
        ' {"visibility":"PUBLIC","description":"test","url":"/services/api/sites/test","sitePreset":"site-dashboard","title":"test","shortName":"test"}'
      );
Then I use POST /share/service/modules/create-website: to populate sitedata on the Share side :

  my $GetRequest2 = HTTP::Request->new
      (
         'POST',
         "http://HOST:PORT/share/service/modules/create-site",
   $h2,
   '{"shortName":"test", "sitePreset" : "site-dashboard"}'
        
      );


(I have some problem with this second stage)
Is this the right approach?
Thank you
Cordially

jitendra1222
Champ in-the-making
Champ in-the-making
I have created site using java backend webscript. Code snippet:
SiteInfo siteInfo = siteService.createSite("site-dashboard", "rma",
            "my site", "my site on share",
            SiteVisibility.PUBLIC);

but when I open site from share dashboard, it shows error on server console :

SEVERE: Servlet.service() for servlet [Spring Surf Dispatcher Servlet] in context with path [/share] threw exception [Could not resolve view with name 'site/rma/dashboard' in servlet with name 'Spring Surf Dispatcher Servlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name 'site/rma/dashboard' in servlet with name 'Spring Surf Dispatcher Servlet'
   at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.MTAuthenticationFilter.doFilter(MTAuthenticationFilter.java:74)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.SecurityHeadersFilter.doFilter(SecurityHeadersFilter.java:168)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.CSRFFilter.doFilter(CSRFFilter.java:313)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.SSOAuthenticationFilter.doFilter(SSOAuthenticationFilter.java:378)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
   at java.lang.Thread.run(Thread.java:722)