Not able to create site programmatically

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 01:08 AM
Hi ,
I trying to create a site in alfresco share using a webscript in java and this i am doing with the help of below code:
HttpClient client = new HttpClient(); PostMethod httpPost = new PostMethod ("http://localhost:8080/share/service/modules/create-site?authority="+username+"&alf_ticket="+alf_tkt); JSONObject site = new JSONObject(); try { site.put("shortName", "java_test"); site.put("Visiblity", "Public"); site.put("sitePreset", "site-dashboard"); site.put("title", "java_test"); site.put("description", "test"); httpPost.setDoAuthentication(true); httpPost.setRequestHeader("Content-Type", "application/json"); httpPost.setRequestEntity( new StringRequestEntity(site.toString(), "application/json", "UTF-8")); int status1 = client.executeMethod(httpPost); if (status1 != HttpStatus.SC_OK) { System.err.println("Method failed: " + httpPost.getStatusLine()); } } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
It is giving me the error as HTTP/1.1 401 Unauthorized.
Please help me in this.
Regards,
Nancy
- Labels:
-
Archive

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 03:15 AM
Hi Nancy, Please make sure your ticket is not expired and you are calling Share script which again internally calls Alfresco data script to create a site and that script also required authentication. So I believe here authentication chain is breaking.
Why dont you directly call Alfresco data script. Just change your script URL to data script as below and it should work.
PostMethod httpPost = new PostMethod ("http://localhost:8080/alfresco/service/api/sites?&alf_ticket="+alf_tkt);
Niketa Patel
Senior Alfresco Consultant
CIGNEX Datamatics

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 03:50 AM
Thanks for your reply. But the above url only creates the site at repository level, do not create a fully functional site in share interface.
Regards,
Nancy Aggarwal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2016 02:04 AM
Either you can create new share side script to create site-dashboard preset or if you need to use share create site module script then as per below wiki you need to pass JSESSIONID for repository authentication.
" if you call a Surf Web Script URL directly in a browser address bar there is no authentication (i.e. no context). You would need to have a JSESSIONID that is already authenticated (i.e. by the page). Surf ties the given JSESSIONID to the Alfresco TICKET that is stored in the session for that user for that connector (i.e. alfresco, alfresco-api). When a client-side library on an authenticated page makes an XHR call to a /service URL it will be passing the JSESSIONID automatically." Reference - http://docs.alfresco.com/5.1/concepts/dev-extensions-share-surf-web-scripts.html

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2016 03:29 AM
It helped me. I got the jsession id and its working, giving me no exception but in alfresco share site is not created.
can you tell where it is breaking?
Regards
Nancy Aggarwal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2016 07:37 AM
I have posted solution here. Please check it - http://niketa-alfresco3.blogspot.in/2016/04/create-alfresco-share-sites-through.html

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 01:51 AM
Thanks for the reply. Actually i am authenticating user by using alf_ticket and creating a session using alf_ticket and username using CMIS api and the part i was missing is making a touch call for validating session. I make the touch call for initiating session and stills the site is not created. I also run your code as a single entity with username and password and it gives me 400 error status for creating site.
What is that i am missing?
Thanks & Regards,
Nancy

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2016 03:03 AM
Also can you post your entire code using CMIS API.
To call/authenticate share script , need to pass JSESSIONID and to get it need to use dologin script. Not sure if there is any other way.
And when you execute dologin script it starts authentication handshale and internally it gets Alfresco ticket. and for next all consecutive Repository level webscript call it internally does append alf_ticket.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2016 02:14 AM
I tried your code as a single code also and it is giving me bad request(the request sent was syntactically incorrect) and when i am trying it in my code it gives me 401 error for touch call share webscript.
HttpClient http = new HttpClient(); PostMethod loginPost = new PostMethod("http://localhost:8080/share/page?pt=login"); String logindata = "username="+user+"&password="+alf_tkt; loginPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); loginPost.setRequestEntity(new StringRequestEntity(logindata, "text/plain", "UTF-8")); System.out.println("dologin script status: " + http.executeMethod(loginPost)); org.apache.commons.httpclient.Header[] headers = loginPost.getResponseHeaders(); Header cookies1 =loginPost.getResponseHeader("Set-Cookie"); // getting JSESSIONID from the header System.out.println("session id ============" +sessionid); GetMethod authenticationGet = new GetMethod("http://localhost:8080/share/service/modules/authenticated?a=user"); authenticationGet.setRequestHeader("JSESSIONID", sessionid); System.out.println("User is authenticated script status : " + http.executeMethod(authenticationGet)); PostMethod httpPost = new PostMethod ("http://localhost:8080/share/service/modules/create-site"); JSONObject site = new JSONObject(); try{ site.put("shortName", "java"); site.put("Visiblity", "PUBLIC"); site.put("sitePreset", "site-dashboard"); site.put("title", "java"); site.put("description", "Google-cURL_next"); httpPost.setRequestHeader("Content-Type", "application/json"); httpPost.setRequestHeader("Accept", "application/json"); httpPost.setRequestEntity(new StringRequestEntity(site.toString(), "application/json", "UTF-8")); int status = http.executeMethod(httpPost); if (status != HttpStatus.SC_OK) { System.err.println("Method failed: " + httpPost.getStatusLine()); } else { System.out.println("site created successfully");}
Regards,
Nancy

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2016 02:28 AM
//PostMethod loginPost = new PostMethod("http://localhost:8080/share/page?pt=login"); PostMethod loginPost = new PostMethod("http://localhost:8080/share/page/dologin"); //USE THIS ONE
