API Questions on Kentico API.
Version 5.x > API > Howto Create BlogPost when BlogMonth doesn't exist? View modes: 
User avatar
Member
Member
Comet Halley - 11/7/2011 8:57:39 PM
   
Howto Create BlogPost when BlogMonth doesn't exist?
Using Kentico 5.5 R2

I would like to import into Kentico a certain number of blog post from an existing blog system. Let's take one random blog post, it's create date was 2008-07-14.

Can you please describe the steps to create a BlogPost and have it placed under the right BlogMonth parent node? There are only a few blog post in the Kentico DB and they were placed under parent node BlogMonth of October 2011 and November 2011. What should I do to have the imported BlogPost node object placed under July 2008 parent node?

Thanks in advance for any help.

User avatar
Member
Member
Comet Halley - 11/9/2011 9:19:54 AM
   
RE:Howto Create BlogPost when BlogMonth doesn't exist?
Hi,

I have tried this solution and it seems to work:

var myBlogPost = new CMS.TreeEngine.TreeNode("CMS.BlogPost", tree);

. . . etc . . . fill in properties & various myBlogPost.SetValue("customFieldName", value)

int blogMonthParentNodeID = CMS.WorkflowEngine.DocumentHelper.EnsureBlogPostHierarchy(myBlogPost, blogRootNode, tree);

CMS.WorkflowEngine.DocumentHelper.InsertDocument(myBlogPost, blogMonthParentNodeID, tree);

In our case, we need some mandatory custom attributes in the BlogMonth document which is created automatically by EnsureBlogPostHierarchy(). To fill these properties, I use CustomTreeNodeHandler.OnBeforeInsert()

User avatar
Kentico Support
Kentico Support
kentico_radekm - 11/11/2011 1:48:52 AM
   
RE:Howto Create BlogPost when BlogMonth doesn't exist?
Hello.

This approach seems to be OK, if ID returned by EnsureBlogPostHierarchy() method (ID of the new parent - blog month document) is correct ID you need. If you would like to insert given blogpost under some existing month, you can get ID of given cms.blogmonth parent from CONTENT_BlogMonth table.

As for the last par you wrote – so you extended cms.blogmonth document type by some additional custom field(s) and you fill them in CustomTreeNodeHandler.OnBeforeInsert() method, which is triggered after you run your custom code, right? So, when the blog post under the corresponding blog month is created, it contains proper value? Where exactly you want to use (get) these custom values, then, please? Can you clarify? Or did you already fix it? Thank you.

Best Regards,
Radek Macalik

User avatar
Member
Member
Comet Halley - 11/11/2011 5:10:22 PM
   
RE:Howto Create BlogPost when BlogMonth doesn't exist?
Sorry for being vague, I had no trouble with CustomTreeNodeHandler.OnBeforeInsert(). I use this method to add the mandatory custom field for the BlogMonth Document.

However, I have uncover another issue. Can you please review and let me know what is the proper handling of the API for this scenario.

I inserted a new BlogPost document, having BlogPostDate = '2008-07-14'. A call to EnsureBlogPostHierarchy() returned me the BlogMonth 'July 2008'. If this BlogMonth didn't exist, the the API created it automatically. So far so good.

Now let's assume the source data of the Blog Content (from the pre-Kentico Blog System) was changed in the mean time. I need to resync it with the BlogPost which was already imported.

Let assume among the BlogPost data to be updated. The BlogPostDate was also modified to '2008-09-14'. May be this doesn't make sense, but let's assume this data is modified. I make a call to EnsureBlogPostHierarchy() then I call CMS.WorkflowEngine.DocumentHelper.UpdateDocument(myBlogPostt, myTree)

I would expect that the BlogPost will now be child node of the BlogMonth 'September 2008'. In fact, it still remains child of 'July 2008'.

Can you please let me know this is the intended behaviour of the API? Meaning, EnsureBlogPostHierarchy() is only used for creating new BlogPost, but not for update?

Thanks in advance.



User avatar
Kentico Support
Kentico Support
kentico_radekm - 12/6/2011 3:40:25 AM
   
RE:Howto Create BlogPost when BlogMonth doesn't exist?
Hello.

Regrettably, this behavior is by design. EnsureBlogPostHierarchy() method is called only when inserting a new blog post, new culture version or via metaweblog API. But it is not called when you just update given blog post. When you do some action for purpose, like changing blog post date, you need to ensure proper blog post’s location (under corresponding blog month) manually, or via some custom code.

Anyway, I did submit it as a requirement, so this EnsureBlogPostHierarchy() method should be called when update document, too. We will consider this implementation to some of future releases.

Best Regards,
Radek Macalik