Im adding an item in a custom module using this api — /public/api/module-items/create, through AJAX. Is it possible to get the details of the newly added item especially the item ID?
Hello @omamai, welcome to the Treepl forum!
There is actually no public API endpoint for creating custom module items at the moment in Treepl - to my knowledge. API is available for CRM Orders as per documentation here: Public API (Orders) - Google Docs
Or did I miss anything, @Adam.Wilson?
1 Like
Hi @omamai
If you add ?jsonResponse=1
to the form action (API endpoint) then you should get the full Liquid object in the AJAX response.
Here’s an AJAX sample I found:
<form id="cmCreate" action="/public/api/module-items/create?jsonResponse=1" onsubmit="createFormSubmit(); return false;" method="post" enctype="multipart/form-data">
<input type="hidden" name="ModuleId" value="1891" required>
<input type="hidden" name="redirectURL" value="">
<label>Name</label>
<input type="text" name="prop_Name" value="" required>
<input type="submit" value="Create" >
</form>
<div id="msg"></div>
<script>
function createFormSubmit() {
var $frm = $('#cmCreate'),
$m = $('#msg');
$.ajax({
type: 'POST',
dataType: "json",
url: $frm.attr('action'),
data: $frm.serialize()
})
.done(function(msg) {
var formResponse = msg;
if (formResponse.Error == 0) {
var msg = "";
for (var i = 0; i < formResponse.Fields.All.length; i++) {
msg += formResponse.Fields.All[i].Name + ': ' + formResponse.Fields.All[i].Value + '<br>';
}
$m.slideDown().html(msg);
} else {
var msg = "";
for (var i = 0; i < formResponse.ErrorMessages.length; i++) {
msg += formResponse.ErrorMessages[i] + "<br>";
}
$m.slideDown().html(msg);
}
})
.fail(function(msg) {
$m.slideDown().html(msg);
});
}
</script>
The JSON response should look something like this:
{
"Fields": {
"System": {
"Name": "Test Item"
},
"Custom": {
"File": null,
"Photo": null
},
"All": [
{
"Name": "Name",
"Value": "Test Item",
"Type": "TextBox"
},
{
"Name": "File",
"Value": null,
"Type": "Media"
},
{
"Name": "Photo",
"Value": null,
"Type": "Media"
}
]
},
"ErrorMessages": [],
"Error": 0,
"ModuleItem": {
"Id": 3873,
"Name": "Test Item",
"Url": "/team/test-item",
"Url_List": [
"/team/test-item"
],
"UrlSlug": "test-item",
"ParentId": 1891,
"ParentId_List": [
-1
],
"ParentName": "",
"ParentUrl": "",
"TemplateName": "",
"Module_Alias": "Team",
"Module_ID": 1891,
"Enabled": true,
"ReleaseDate": "2023-01-25T08:56:01.907261",
"ExpiryDate": "9999-12-31T23:59:59.999999",
"SiteSearchKeywords": [],
"Description": "",
"Weighting": 0,
"DisableForSiteSearch": false,
"CreatedByMemberId": "1843",
"ItemCategories": [],
"ItemCategoryIdList": [],
"ItemTags": [],
"Author": 0,
"Author_Name": "",
"Author_Url": "",
"Item_Rating": 0,
"Photo": "",
"Position": "",
"ShortDescription": "",
"Facebook": "",
"Instagram": "",
"Tumblr": "",
"Twitter": "",
"Phone": "",
"Email": "",
"DescriptionTeam": "",
"NumNum": 0,
"Bool": false,
"Checky": [],
"MyDataSource": 0,
"File": "",
"CRM": "",
"MyField": "",
"Selector": "",
"ShowPageForSearchEngine": false,
"MetaTitle": "",
"SEOTitle": "",
"MetaDescription": "",
"CanonicalLink": "",
"SocialMetaTags": "",
"SeoPriority": 0.5,
"EnableAMP": false,
"AMPContent": "",
"OpenGraphProperties": {
"title": null,
"type": null,
"url": null,
"locale": null,
"image": null
},
"ExternalId": 0,
"Params": {}
}
}
1 Like
It’s now working. Thank you @Adam.Wilson!
1 Like