Create multilingual Dynamic Content items (including bugfixes)

If you need to create Dynamic Content Items through code, like I did a few days ago, you probably can't find any documentation or sample code. That is because there isn't any.

  • December 10, 2018

Background information

If you need to create Dynamic Content Items through code, like I did a few days ago, you probably can't find any documentation or sample code. That is because there isn't any.

And if you finally know how to create multilingual versions of Dynamic Content items, it will fail you, since there is a known bug in Sitefinity that gives you troubles.

Of course, with the great help of Sitefinity support, I could download a fix and all works fine now.

Requirements

The requirements for my little project were to import projects that had been created with an older custom module, back in the days of Sitefinity 4.x. I had to get the data out of this module and insert it into my new Dynamic Module, which is way better and easier to implement. If you still run on older versions of Sitefinity, I recommend to migrate old custom modules to Dynamic Modules (if they are not TOO complex of course) and if you still run your site on Sitefinity 3.7, I also recommend to migrate to the latest version of Sitefinity.

So, I wanted to import the old data and create new Dynamic Content items with their different languages at the same time.

Solution

This is the code you need:

foreach (var project in projects) {

                Thread.CurrentThread.CurrentUICulture = nlCulture;
                var projectItem = dynamicModuleManager.CreateDataItem(projectType);

                using (new CultureRegion(nlCulture)) {

                    projectItem.SetString("Title", project.Title, nlCulture);
                    projectItem.SetString("Description", project.ProjectContent, nlCulture);
                    projectItem.SetString("Goal", project.ProjectGoal, nlCulture);
                    projectItem.SetString("Summary", project.ProjectDetail, nlCulture);
                    projectItem.SetValue("Budget", project.ProjectBudget);
                    projectItem.SetString("UrlName", Regex.Replace(project.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"), nlCulture);

                    projectItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published", nlCulture);

                    dynamicModuleManager.SaveChanges();
                    dynamicModuleManager.Lifecycle.Publish(projectItem);
                    dynamicModuleManager.SaveChanges();
                }

                using (new CultureRegion(enCulture)) {

                    if (project.Title_EN.IsNullOrEmpty()) continue;

                    Thread.CurrentThread.CurrentUICulture = enCulture;
                    projectItem.SetString("Title", project.Title_EN, enCulture);
                    projectItem.SetString("Description", project.ProjectContent_EN, enCulture);
                    projectItem.SetString("Goal", project.ProjectGoal_EN, enCulture);
                    projectItem.SetString("Summary", project.ProjectDetail_EN, enCulture);
                    projectItem.SetString("UrlName", Regex.Replace(project.Title_EN.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"), enCulture);

                    projectItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published", enCulture);
                    dynamicModuleManager.SaveChanges();
                    dynamicModuleManager.Lifecycle.Publish(projectItem);
                    dynamicModuleManager.SaveChanges();

                }
            }

There unfortunately was an error that was raising everytime I tried to add the english language and in particular when adding content to a field of type LString (a LongText). The error that raises is:  

Expected instance of type Telerik.Sitefinity.DynamicTypes.Model.Projects.Project
Parameter name: component
Actual value was Telerik.Sitefinity.DynamicTypes.Model.Projects.Project.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Expected instance of type Telerik.Sitefinity.DynamicTypes.Model.Projects.Project
Parameter name: component
Actual value was Telerik.Sitefinity.DynamicTypes.Model.Projects.Project.

In the end it was approved as a bug in Sitefinity.

Bugfix

The bug will be fixed in Sitefinity 6.3. Until then, you can download this fix of the Telerik.Sitefinity.Model.dll if you need to work with multilingual versions of your Dynamic Modules.

The .dll is for version Sitefinity 6.2.4910. You have to include it in your project and then build it. Please make sure you make a backup of your original .dll  and try this solution on a backup of your project first.


Image Description

Daniel Plomp

Entrepreneur, Senior Software Engineer .NET, Sitefinity Solution specialist, Orchard CMS enthusiast, Product Owner

All Author Posts

Got another minute? Check out:

Image Description

Call us

+31 (0) 36-2000196
Image Description

Email us

mail@zimplerconsulting.com
Image Description

Address

Grote Beer 57, 3893 DJ Zeewolde
NLThe Netherlands

Please sign in

Signin to manage your account.

Do not have an account? Signup

OR