API Questions on Kentico API.
Version 6.x > API > Creating new document with rest api View modes: 
User avatar
Member
Member
lee.gould-redington.co - 1/23/2013 10:33:34 AM
   
Creating new document with rest api
Hi, wonder if you can give me some pointers on using the rest service to create documents using json.

I have setup the rest service on the ver 6 of kentico we have running, and have successfully got get and update working against a custom document, but creating new documents has left me a bit confused. The Kentico 6 documentation just has 1 example in xml;
POST http://localhost/CMS/rest/content/site/CorporateSite/en-us/document/Home HTTP/1.1
User-Agent: Fiddler
Authorization: Basic <enter Base64-encoded <username>:<password> here>
Host: localhost
Content-Type: text\xml
Content-Length:165

<CMS_MenuItem>
<NodeClassID><enter class ID here></NodeClassID>
<DocumentName>Services</DocumentName>
<DocumentPageTemplateID><enter template ID here></DocumentPageTemplateID>
</CMS_MenuItem>

So I'm trying to do something similar, and whatever I try seems to give me an event log error saying :
TreeNode.NewInstance]: Class name not defined!

I've tried sending without any wrapper json:
{
"NodeClassID": 2143,
"DocumentPageTemplateID": 256
"DocumentName": "Lees Test Org",
"ClassName": "customtable.Organisation",
"Name": "Lees Test Org",
}

and then with variations of the wrappers that come back in the queries (wrapped in "cms_documents" array then "customtable_Organisation" array, or the "data" array from a post request I saw that I think was to do with the object service), without much success.

Is there something else I need to be sending to get it to create documents successfully?

TIA,
Lee.

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 2/20/2013 8:02:01 AM
   
RE:Creating new document with rest api
Hi,

After many different tests, it seems that only reason is the incorrect JSON data.
Is the ClassName from the JSON data a class name for the class with ID 2143?

I've been able to create documents using JSON in the following pattern (using your data):
{"NodeClassID":2143,"DocumentPageTemplateID":256,"DocumentName":"JSONTest"}

No ClassName or Name needed...

Have you found the correct pattern in your attempts? I know it's been a while, I'm sorry for not answering this earlier.

Thank you in advance for information.

Please let me know if you still need advice with finding a solution.
Regards, Zdenek.

User avatar
Member
Member
lee.gould-redington.co - 2/20/2013 8:49:12 AM
   
RE:Creating new document with rest api
Hi Zdenek,

Thanks for having a look at this.

Yea, the data is definatly valid. I coded around the problem by forcing my wrapper to send the create request in xml for just those create requests, which worked fine with the same data. I would love to know why that was like that, but I ran out of time and had to move on.

Thanks anyway! :)

Lee.

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 2/21/2013 9:11:51 AM
   
RE:Creating new document with rest api
Hi Lee,

Thanks for the reply and info. I'm really sorry I didn't replied earlier.
The data without wrappers really worked fine for me, but wider columns data specification may cause the REST won't accept them.

If you're still interested in the json solution, please let me know.

Regards,
Zdenek.


User avatar
Member
Member
lee.gould-redington.co - 4/17/2013 9:55:42 AM
   
RE:Creating new document with rest api
Hi, I have some new info for anyone else experiencing this problem (It was probably just me to be honest!). Months later while digging into another problem, I noticed what was causing this problem.

Inside CMS.CMSHelper.CMSHierarchyHelper there is a static bit of regex used for parsing the NodeClassID from xml/json. The JSON one, called NodeClassJSONRegex, is set to "\"NodeClassID\":(?<classid>[0-9]+)", which does not allow any whitespace after the colon. Therefore if you want to make JSON create requests, you need to make sure there is no whitespace here.. i.e. "NodeClassID":1234.

HTH someone else :)

Thanks,
Lee.

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 4/24/2013 11:15:53 PM
   
RE:Creating new document with rest api
Hi,

That's an interesting point!
Thanks for the information, I'll talk to developers and technical writers to check whether it's properly mentioned in instructions or ask about possibility to allow whitespace chars.

Regards,
Zdenek