cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a share site using the REST API ?

ferbru60
Champ in-the-making
Champ in-the-making
I try to create (using PERL) a share site using the REST API. I read the doc http://wiki.alfresco.com/wiki/3.0_REST_API#Site_Service and I coded that :

use warnings;
use strict;

use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;

use JSON;

my $alfTicket;

my $host = 'localhost';
my $port = 8080;

sub login ()
{
   my ($username, $password) = @_;

   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/login",
         $h,
         '{"username" : "' . $username . '","password" : "' . $password . '"}'
      );
   my $res = $ua->request($GetReq);
    # Check the outcome of the response
    if ($res->is_success)
    {
      my $hashRef = JSON::decode_json ($res->content);
      $alfTicket = $hashRef->{"data"}->{"ticket"};
        return 1;
    }
    else
    {
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
        return 0;
    }
}

sub createSite ()
{
   my ($siteInfosRef) = @_;
   my $content = JSON::encode_json ($siteInfosRef);
   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/sites?alf_ticket=$alfTicket",
         $h,
         $content
      );
   print "—————————-\n";
   print "Request :\n" . $GetReq->as_string . "\n";
   print "—————————-\n";
   my $res = $ua->request($GetReq);
    if ($res->is_success)
    {
      %{$siteInfosRef} = %{JSON::decode_json ($res->content)};
        return 1;
    }
    else
    {
      print "—————————-\n";
      print "Response :\n";
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
      print "—————————-\n";
        return 0;
    }
}

print "username: ";
my $username = <STDIN>;
chomp ($username);

print "password: ";
my $password = <STDIN>;
chomp ($password);

if (&login ($username, $password))
{
   print "Site creation :\n";
   print "Short name : ";
   my $shortName = <STDIN>;
   chomp ($shortName);
   print "Description : ";
   my $description = <STDIN>;
   chomp ($description);
   print "Title : ";
   my $title = <STDIN>;
   chomp ($title);
   
   my %siteInfos;
   $siteInfos {"visibility"} = 'PRIVATE';
   $siteInfos {"shortName"} = $shortName;
   $siteInfos {"description"} = $description;
   $siteInfos {"title"} = $title;
   $siteInfos {"sitePreset"} = 'site-dashboard';

   print "$username is logged on !\n";
   if (&createSite (\%siteInfos))
   {
      print "$shortName created !\n";
   }
   else
   {
      print "Can't create site $shortName\n";
   }
}
else
{
   print "$username can't log in $host:$port\n";
}

An the results are :
—————————-
Request :
POST http://localhost:8080/alfresco/service/api/sites?alf_ticket=TICKET_f476c578a1740b4753eb05d146120ec7e...
Accept: text/x-json
Content-Type: text/x-json

{"visibility":"PRIVATE","sitePreset":"site-dashboard","title":"My Site","description":"One more site !!!","shortName":"mybfsite"}

—————————-
—————————-
Response :
Error (500) : Erreur Interne de Servlet
   {
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "09030197 Wrapped Exception (with status template): 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator.", 
  "exception" : "org.alfresco.web.scripts.WebScriptException - 09030197 Wrapped Exception (with status template): 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator.",
 
  "callstack" :
  [
       ""      ,"freemarker.core.InvalidReferenceException: Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl."
      ,"freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)"
      ,"freemarker.core.Expression.getStringValue(Expression.java:118)"
      ,"freemarker.core.AddConcatExpression._getAsTemplateModel(AddConcatExpression.java:98)"
      ,"freemarker.core.Expression.getAsTemplateModel(Expression.java:89)"
      ,"freemarker.core.ListLiteral.getModelList(ListLiteral.java:119)"
      ,"freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:89)"
      ,"freemarker.core.Expression.getAsTemplateModel(Expression.java:89)"
      ,"freemarker.core.Expression.getStringValue(Expression.java:93)"
      ,"freemarker.core.DollarVariable.accept(DollarVariable.java:76)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:92)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.Macro$Context.runMacro(Macro.java:164)"
      ,"freemarker.core.Environment.visit(Environment.java:601)"
      ,"freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:92)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.Environment.process(Environment.java:188)"
      ,"freemarker.template.Template.process(Template.java:237)"
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:202)"
      ,"org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:523)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:241)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:357)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:326)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:407)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:424)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:288)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:262)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)"
      ,"org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)"
      ,"org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)"
      ,"org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)"
      ,"java.lang.Thread.run(Thread.java:619)"
      ,"org.alfresco.service.cmr.repository.TemplateException: 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator."
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:206)"
      ,"org.alfresco.web.scripts.WebScriptException: 09030197 Wrapped Exception (with status template): 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator."
      ,"org.alfresco.web.scripts.AbstractWebScript.createStatusException(AbstractWebScript.java:613)"

  ],
 
  "server" : "Alfresco Community v3.2.0 (2039) schema 2 019",
  "time" : "3 oct. 2009 11:24:58"
}


—————————-
Can't create site mybfsite


Why "It" says that there's an empty shortName ? Someone may help me ?

Sorry for my poor english I'm french !

Bruno
6 REPLIES 6

mikeh
Star Contributor
Star Contributor
For both your questions, you need Content-Type to be "application/json", not "text/x-json"

Thanks,
Mike

ferbru60
Champ in-the-making
Champ in-the-making
Ok. If I replace

$h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');

with

$h->header ('Content-Type' => 'application/json', Accept=>'application/json');

I can successfuly create a Share Site. And I thank you for your help.

But, there's still some problems…

With Share GUI I can not manage this new site. And if I look Sites with Alfresco GUI I can see my new created site but this one doesn't contains anything (no "links" space and no "documentLibrary" space).

My question now is : how can I create a site with the REST API similar to one created with the Share GUI ?

Bruno

mikeh
Star Contributor
Star Contributor
You need to create the AVM assets too. Please see http://forums.alfresco.com/en/viewtopic.php?f=48&t=18046 or follow what the current Create Site functionality does in Share.

Thanks,
Mike

ferbru60
Champ in-the-making
Champ in-the-making
So, I've tried lot of things… And I come back to you with… a problem !

I've modified my code according to MikeH and now I coded that

use warnings;
use strict;

use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;

use JSON;

my $alfTicket;

my $host = 'localhost';
my $port = 8080;

sub login ()
{
   my ($username, $password) = @_;

   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'application/json', Accept=>'application/json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/login",
         $h,
         '{"username" : "' . $username . '","password" : "' . $password . '"}'
      );
   my $res = $ua->request($GetReq);
    # Check the outcome of the response
    if ($res->is_success)
    {
      my $hashRef = JSON::decode_json ($res->content);
      $alfTicket = $hashRef->{"data"}->{"ticket"};
        return 1;
    }
    else
    {
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
        return 0;
    }
}

sub createShareSite ()
{
   my ($siteInfosRef) = @_;
   my $content = JSON::encode_json ($siteInfosRef);
   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'json', Accept=>'json');
   my $req = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/share/service/modules/create-site?alf_ticket=$alfTicket",
         $h,
         $content
      );
   print "—————————-\n";
   print "Request :\n" . $req->as_string . "\n";
   print "—————————-\n";
   my $res = $ua->request($req);
   print "++++++++++++++++++++++++++++\n";
   print "Response :\n" . $res->as_string . "\n";
   print "++++++++++++++++++++++++++++\n";
    if ($res->is_success)
    {
      %{$siteInfosRef} = %{JSON::decode_json ($res->content)};
        return 1;
    }
    else
    {
      print "—————————-\n";
      print "Response :\n";
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
      print "—————————-\n";
        return 0;
    }
}

print "username: ";
my $username = <STDIN>;
chomp ($username);

print "password: ";
my $password = <STDIN>;
chomp ($password);

if (&login ($username, $password))
{
   print "Site creation :\n";
   print "Short name : ";
   my $shortName = <STDIN>;
   chomp ($shortName);
   print "Description : ";
   my $description = <STDIN>;
   chomp ($description);
   print "Title : ";
   my $title = <STDIN>;
   chomp ($title);
  
   my %siteInfos;
   $siteInfos {"visibility"} = 'PRIVATE';
   $siteInfos {"shortName"} = $shortName;
   $siteInfos {"description"} = $description;
   $siteInfos {"title"} = $title;
   $siteInfos {"sitePreset"} = 'site-dashboard';

   print "$username is logged on !\n";
   if (&createShareSite (\%siteInfos))
   {
      print "$shortName created !\n";
   }
   else
   {
      print "Can't create site $shortName\n";
   }
}
else
{
   print "$username can't log in $host:$port\n";
}

But, when I run this code I get errors.
1) On the client side I get a HTTP 500 error invited me to see the server logs…
2) On the Share+Alfresco Server side, Tomcat complains :
INFO: Server startup in 100711 ms
09:24:28,928  ERROR [freemarker.runtime]

Expression success is undefined on line 2, column 17 in org/alfresco/modules/cre
ate-site.post.json.ftl.
The problematic instruction:
———-
==> ${success?string} [on line 2, column 15 in org/alfresco/modules/create-site.
post.json.ftl]
———-

Java backtrace for programmers:
———-
freemarker.core.InvalidReferenceException: Expression success is undefined on li
ne 2, column 17 in org/alfresco/modules/create-site.post.json.ftl.
        at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)

        at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.ja
va:134)
        at freemarker.core.BuiltIn$stringBI._getAsTemplateModel(BuiltIn.java:361
)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.Expression.getStringValue(Expression.java:93)
        at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.Environment.process(Environment.java:188)
        at freemarker.template.Template.process(Template.java:237)
        at org.alfresco.web.scripts.PresentationTemplateProcessor.process(Presen
tationTemplateProcessor.java:185)
        at org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWeb
Script.java:523)
        at org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(De
clarativeWebScript.java:241)
        at org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebS
cript.java:147)
        at org.alfresco.web.scripts.PresentationContainer.executeScript(Presenta
tionContainer.java:60)
        at org.alfresco.web.scripts.LocalWebScriptRuntimeContainer.executeScript
(LocalWebScriptRuntimeContainer.java:186)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntim
e.java:262)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntim
e.java:139)
        at org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptSe
rvlet.java:122)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:845)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:44
7)
        at java.lang.Thread.run(Thread.java:619)
09:24:28,959  ERROR [web.scripts.AbstractRuntime] Exception from executeScript -
redirecting to status template error: 09140000 Failed to process template org/a
lfresco/modules/create-site.post.json.ftl
org.alfresco.web.scripts.WebScriptException: 09140000 Failed to process template
org/alfresco/modules/create-site.post.json.ftl
        at org.alfresco.web.scripts.PresentationTemplateProcessor.process(Presen
tationTemplateProcessor.java:189)
        at org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWeb
Script.java:523)
        at org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(De
clarativeWebScript.java:241)
        at org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebS
cript.java:147)
        at org.alfresco.web.scripts.PresentationContainer.executeScript(Presenta
tionContainer.java:60)
        at org.alfresco.web.scripts.LocalWebScriptRuntimeContainer.executeScript
(LocalWebScriptRuntimeContainer.java:186)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntim
e.java:262)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntim
e.java:139)
        at org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptSe
rvlet.java:122)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:845)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:44
7)
        at java.lang.Thread.run(Thread.java:619)
Caused by: freemarker.core.InvalidReferenceException: Expression success is unde
fined on line 2, column 17 in org/alfresco/modules/create-site.post.json.ftl.
        at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)

        at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.ja
va:134)
        at freemarker.core.BuiltIn$stringBI._getAsTemplateModel(BuiltIn.java:361
)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.Expression.getStringValue(Expression.java:93)
        at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.Environment.process(Environment.java:188)
        at freemarker.template.Template.process(Template.java:237)
        at org.alfresco.web.scripts.PresentationTemplateProcessor.process(Presen
tationTemplateProcessor.java:185)
        … 21 more

I have never modified create-site.post.json.ftl. This file contains :
{
   "success": ${success?string}
<#if code?exists>, "code": ${code}</#if>
<#if error?exists>, "error": "${error}"</#if>
}

My Share+Alfresco server runs on Windows 2003 Server R2… If someone can help me…

Thanks

Bruno

sans
Champ in-the-making
Champ in-the-making
Hi Bruno,
Did you find the solution to your problem?
I am also having the same issue. I am getting 500 error on client side and on server side I am getting
Expression success is undefined on line 2, column 17 in org/alfresco/modules/create-site.post.json.ftl
.

Please let me know if you find anything on this.

Thanks
Sans!!

joemc3
Champ in-the-making
Champ in-the-making
I recently had the same problem.  Posted my solution here:

https://forums.alfresco.com/en/viewtopic.php?f=48&t=46259&p=138469#p138469