Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Last active December 23, 2015 06:39
Show Gist options
  • Save cfalzone/6595248 to your computer and use it in GitHub Desktop.
Save cfalzone/6595248 to your computer and use it in GitHub Desktop.
Creating a contentlet in 2.5 from json
JSONObject data = new JSONObject(dataString);
JSONArray contentlets = data.getJSONArray("contentlets");
for(int i = 0; i<contentlets.length(); i++) {
JSONObject con = contentlets.getJSONObject(i);
Contentlet contentlet=new Contentlet();
contentlet.setStructureInode(stInode);
contentlet.setHost(stHost.getIdentifier());
long languageId = con.getLong("languageId");
contentlet.setLanguageId(languageId);
JSONArray keys = con.names();
for(int j = 0; j<keys.length(); j++) {
String key = keys.getString(j);
String value = con.getString(key);
Field f = fieldMap.get(key);
if(f == null) continue;
conAPI.setContentletProperty(contentlet, f, value);
}
try {
contentlet = conAPI.checkin(contentlet, userAPI.getSystemUser(), false);
conAPI.publish(contentlet, userAPI.getSystemUser(), false);
} catch (Exception e) {
failed.put(stVarName, "Unable to checkin and publish Contentlet: "+e.getMessage());
throw new Exception(e);
}
}
@cfalzone
Copy link
Author

Note that if you are actually going to use this you need to do some exception handling on the JSONObject methods. It is using org.json.JSONObject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment