Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Create new Document type with "BlogMonth" functionality View modes: 
User avatar
Certified Developer 11
Certified Developer 11
felix.planjer-yellowred - 9/3/2010 9:24:53 AM
   
Create new Document type with "BlogMonth" functionality
Hi,

I'm in the process of creating a new "news" document type, and I would like news to be automatically archived under a Month Document type. This is very similar (if not exactly the same) as the Blog/BlogMonth/BlogPost structure.

I've been looking everywhere to figure out how this is implemented in the Blog Module, but cannot find it.

Can someone provide some pointers where to look?

regards, Felix

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 9/6/2010 5:57:36 AM
   
RE:Create new Document type with "BlogMonth" functionality
Hi,

You will need to use custom event handler to handle document events in tree node handler. You will check the document type of the currently created document (e.g. OnBeforeInsert event) and create it under existing month or create new month document first. Below is a code sample fro handling cms.blogpost document type, so you can tak inspiration:

    // Special treatment for blog post (ensure parent month)
if (newdocument && (ci != null) && (ci.ClassName.ToLower() == "cms.blogpost"))
{
// Get parent node
node = tree.SelectSingleNode(nodeId, TreeProvider.ALL_CULTURES);
if (node != null)
{
// Get parent blog ID when inserting post under blog month
if (node.NodeClassName.ToLower() == "cms.blogmonth")
{
node = tree.SelectSingleNode(node.NodeParentID, TreeProvider.ALL_CULTURES);
if (node != null)
{
parentId = node.NodeID;
}
}
// Get parent blog ID when inserting post under blog
else
{
parentId = nodeId;
}

if (node != null)
{
CultureInfo cultureInfo = new CultureInfo(CMSContext.PreferredCultureCode);

// Prepare the data
DateTime startDate = ValidationHelper.GetDateTime(formElem.BasicForm.GetFieldValue("BlogPostDate"), DateTimePicker.NOT_SELECTED);
string monthName = startDate.ToString("MMMM", cultureInfo.DateTimeFormat) + " " + startDate.Year;
// UpperCase first letter
monthName = monthName.Substring(0, 1).ToUpper() + monthName.Substring(1);
startDate = new DateTime(startDate.Year, startDate.Month, 1);

// Check existing
DataSet existingDS = tree.SelectNodes(CMSContext.CurrentSiteName, node.NodeAliasPath + "/%", TreeProvider.ALL_CULTURES, true, "cms.blogmonth", "BlogMonthStartingDate = '" + TableManager.GetDateTimeString(startDate) + "'", null, -1, false);
if (DataHelper.DataSourceIsEmpty(existingDS))
{
// Create the month node
node = new TreeNode("CMS.BlogMonth", tree);
node.DocumentCulture = CMSContext.PreferredCultureCode;

// Insert the month
node.SetValue("BlogMonthName", monthName);
node.SetValue("BlogMonthStartingDate", startDate);
node.DocumentName = monthName;
DocumentHelper.InsertDocument(node, parentId, tree);

formElem.NodeId = node.NodeID;
}
else
{
// Set existing
formElem.NodeId = ValidationHelper.GetInteger(existingDS.Tables[0].Rows[0]["NodeID"], 0);
}
}
}
}



Or, you can also add similar code to \CMSModules\Content\CMSDesk\Edit\Edit.aspx.cs file (but there is a risk that it will be overwritten by future upgrade).

Best regards,
Juraj Ondrus

User avatar
Certified Developer 11
Certified Developer 11
felix.planjer-yellowred - 9/7/2010 8:54:37 AM
   
RE:Create new Document type with "BlogMonth" functionality
Hi Juraj,

Thanks for the quick reply.

I have this almost working. The new NewsMonth document is created (if needed), but the problem is, that I cannot place the new News document under the new NewsMonth.

I use: node.NodeParentID = newParentNode.NodeId

How can I change the parent of a Document in the OnBeforeInsert.OnBeforeInsert method?

regards, Felix

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 9/8/2010 7:07:53 AM
   
RE:Create new Document type with "BlogMonth" functionality
Hi,

Are you getting any errors?
Is the parent node - NewsMonth document created correctly?
right after it is created, you need to get its ID which you can use as the parent ID for the news document.

Best regards,
Juraj Ondrus

User avatar
Certified Developer 11
Certified Developer 11
felix.planjer-yellowred - 9/8/2010 7:42:44 AM
   
RE:Create new Document type with "BlogMonth" functionality
Yes the Month document is created. When debugging I can see that the line:

node.NodeParentID = newParentNode.NodeId

Does set the Parent ID.

However, after saving, in CMSDesk I see both the Month and News document under the News page, and nog nested.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 9/9/2010 4:16:35 AM
   
RE:Create new Document type with "BlogMonth" functionality
Hi,

It seems to be more complicated. Anyway, I have an idea. You will create the nodes as they are right now, on the same level. However, you know the ID of the month document, so in OnAfterSave event for the news document type, you need change the NodeParentID to the month document ID and call update on that document (if it is newly created news document). I hope it makes sense.

Best regards,
Juraj Ondrus

User avatar
Certified Developer 11
Certified Developer 11
felix.planjer-yellowred - 9/15/2010 7:53:28 AM
   
RE:Create new Document type with "BlogMonth" functionality
Hi Jurajo,

Moving the code to the OnAfterSave event worked great!

Thanks for your help.

Regards, Felix