8 YEARS AGO

API: Creating a document in multiple cultures

I have been doing some Kentico API coding lately and I thought that it would be good to share some code which I recently used to create documents in multiple cultures. I will start with a brief overview of the needed steps followed with some example code.

To create documents in multiple cultures via Kentico's API you start by creating a new node (based on a document type) and set the nodes DocumentCulture to the culture in which you want to create the document. The next thing that you need to do is set the values via SetValue on the node and add the DocumentName. Insert the document by calling the InsertDocument function on the DocumentHelper and pass in the node, the parent node id (this is the node id of the document under which you want to add the new document) and the treeprovider.

Now you can use the inserted node to create a new culture version of the document. The steps are pretty much the same: set the nodes DocumentCulture, set the values and specify the DocumentName. Instead of inserting the document, call InsertNewCultureVersion and pass in the node, treeprovider and specify if you want to create the version.

Below I posted code to create a News document in two cultures: English and Dutch.
int parent = 0;

CMS.TreeEngine.TreeProvider tree = 
    new CMS.TreeEngine.TreeProvider(CMSContext.CurrentUser);

CMS.TreeEngine.TreeNode node = 
    new CMS.TreeEngine.TreeNode("CMS.News", tree);

tree.UseCustomHandlers = false;

// English version
node.DocumentCulture = "en-US";
node.SetValue("NewsTitle", "Title");
node.SetValue("NewsReleaseDate", DateTime.Now);
node.SetValue("NewsSummary", "Summary");
node.SetValue("NewsText", "Text");
node.DocumentName = "Title";        
CMS.WorkflowEngine.DocumentHelper.InsertDocument(node, parent, tree);

// Dutch version
node.DocumentCulture = "nl-NL";
node.SetValue("NewsTitle", "Titel");
node.SetValue("NewsReleaseDate", DateTime.Now);
node.SetValue("NewsSummary", "Samenvatting");
node.SetValue("NewsText", "Tekst");
node.DocumentName = "Titel";
CMS.WorkflowEngine.DocumentHelper.InsertNewCultureVersion(node, tree, true);
After running the above code you should get the following results:


English news document

English


Dutch news document

Dutch