API Questions on Kentico API.
Version 6.x > API > Adding a folder upon page creation View modes: 
User avatar
Member
Member
Wolfworks - 1/4/2012 6:59:42 AM
   
Adding a folder upon page creation
Hi,

When a Page is created, I would like to create an additional folder, which can be used to store images. These images can be read from the page and will be displayed in a jQuery image slider, if images are available. Each page can have it's own specific editor, so I would like it to be as automated as possible, and not having implicit functionalities like "create a folder with this name in order to display images".

I created the following code, as mentioned in http://devnet.kentico.com/docs/devguide/index.html?event_handlers_overview.htm:

[ImageFolderProviderModuleLoader]
public partial class CMSModuleLoader
{
/// <summary>
/// Module registration
/// </summary>
private class ImageFolderProviderModuleLoader : CMSLoaderAttribute
{
/// <summary>
/// Initializes the module
/// </summary>
public override void Init()
{
DocumentEvents.Insert.After += Document_Insert_After;
}

private void Document_Insert_After(object sender, DocumentEventArgs e)
{
if (e.Node != null)
{
// First create a folder
var folder = TreeNode.New("CMS.Folder", e.TreeProvider);

folder.DocumentName = "Afbeeldingen";
folder.DocumentCulture = "nl-NL";

folder.Insert(e.Node.NodeID);
}
}
}
}


However, when I try to add a new page, I get a "String or binary data would be truncated. The statement has been terminated." on folder.Insert(e.Node.NodeID) (or folder.Insert(e.Node) for that matter).

Is this the problem: from http://devnet.kentico.com/FAQs/API-and-Internals/Why-the-inserted-document-isn%E2%80%99t-accessible-in-the-.aspx: "The inserted node is not available at that moment because the transaction on the SQL server is still running at the moment when the event fires."?

However, I want to decouple my code from the UI, I don't want to resort into "hacking" in the existing pages, like the solution proposes: "You can try to ensure this in the following file which contains the code for creating new documents from the administration interface: ~\CMSDesk\Content\edit.aspx.cs" (which is in Kentico 6 in ~/CMSModules/Content/CMSDesk/New/new.aspx).

What is the proper way to resolve this? Is there an event after DocumentEvents.Insert.After when the document is available and the transaction is closed? Do I have to call DocumentEvents.Insert.FinishEvent();? Or am I missing something obvious?


User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/5/2012 2:01:44 AM
   
RE:Adding a folder upon page creation
Hi,

One option is to edit the admin interface page as already mentioned or, second option is to create custom pages for creating the document (the page you see when creating a document of some type - e.g. page menu item type has different page then all others).

You can set the custom page in the document type properties as the "new page". For inspiration, you can check the Page menu item type to see how it's made.

And in this custom page you will commit the transaction and then create the new document using the API.

Best regards,
Juraj Ondrus