cancel
Showing results for 
Search instead for 
Did you mean: 

create a site in alfresco

yosrioh
Confirmed Champ
Confirmed Champ
hello,
i am trying to create a site in alfresco by using WEB SCRIPT
" POst http://localhost:8080/share/service/modules/create-site"
i tested this java code that generates no errors and seems to execute fine but when i check in the alfresco dashboard the site is not created

public class TestShareCreateSitePost {

   public static void main(String[] args) throws Exception {
      System.out.println(createSite());
   }

   private static String createSite() {
      try {
         DefaultHttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(
               "http://localhost:8080/share/page/dologin");
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
         nameValuePairs.add(new BasicNameValuePair("username", "admin"));
         nameValuePairs.add(new BasicNameValuePair("password", "admin"));
         post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         client.execute(post);
         CookieStore cookieStore = client.getCookieStore();
         client = new DefaultHttpClient();
         client.setCookieStore(cookieStore);
         post = new HttpPost(
               "http://localhost:8080/share/service/modules/create-site");
         post.setHeader("Content-Type", "application/json");
         post.setEntity(new StringEntity(createSiteParameters(
               "JavaTest-001", "Sito di test by JAVA", "JavaTest-001"),
               "UTF-8"));
         ResponseHandler<String> responseHandler = new BasicResponseHandler();
         String response = client.execute(post, responseHandler);
         return response;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return "Error!";
   }

   private static String createSiteParameters(String title,
         String description, String shortName) {
      String site = "{\"visibility\":\"PUBLIC\", "
            + "\"sitePreset\":\"site-dashboard\", " + "\"title\":\""
            + title + "\", " + "\"description\":\"" + description + "\", "
            + "\"shortName\":\"" + shortName + "\"}";
      System.out.println(site);
      return site;
   }
}

i have also made the modification to disable the CSRF filter but it didn't work also
can someone tell me what im i missing ?
thx
2 REPLIES 2

niketapatel
Star Contributor
Star Contributor
Hi, Everything looks ok in your code just call inbetween authentication script . I have posted solution here - http://niketa-alfresco3.blogspot.in/2016/04/create-alfresco-share-sites-through.html

hello thx for your answer i managed to get the java code work fine but i want to make a python version of the code i tried this

import json
import requests
from json import JSONEncoder

try:
    url = 'http://127.0.0.1:8080/share/page/dologin'
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    post_data = {'username':'admin', 'password':'admin'}
    r = requests.post(url, headers=headers,data=post_data)
    r.encoding = 'UTF-8'
    print r.status_code
except:
    print 'hhhh'
   
   
   
get_response = requests.get('http://127.0.0.1:8080/share/service/modules/authenticated?a=user')
print get_response.status_code
url1='http://127.0.0.1:8080/share/service/modules/create-site'


shortName="sitepython"
jsonString = JSONEncoder().encode({
  "shortName": shortName,
  "Visiblity": "Public",
  "sitePreset": "site-dashboard",
  "title" : shortName,
  "description" : shortName
  })
headers = {'content-type': 'application/json',"Accept":"application/json"}
site = requests.post(url1,headers=headers,data=json.dumps(jsonString))
print site.status_code




and i get 200 for the first request 401 for the second and 500 for the last one can you help me plz