We are trying to create Sitefinity dynamic content programatically but we are not able to do it. We are facing some issues and would like to know if you can help us to get around them. We are using Sitefinity 10.2.6604.0 version and we have created a module and a Type to store some data. We've picked code snipets provided by Sitefinity and whatever we try, we end up always with exceptions being trigger.
Here is the code we have defined:
var providerName = "OpenAccessProvider";
//// Set a transaction name and get the version manager
var transactionName = Guid.NewGuid().ToString();
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName, transactionName);
dynamicModuleManager.Provider.SuppressSecurityChecks = true;
var versionManager = VersionManager.GetManager(null, transactionName);
versionManager.Provider.SuppressSecurityChecks = true;
List<DynamicContent> dynamicContents = new List<DynamicContent>();
//list of agencies
for (int i = 0; i < agencies.Length; i++)
var defaultAgency = agencies[i];
var agencyItem = dynamicModuleManager.CreateDataItem(AgencyType);
agencyItem.UrlName = defaultAgency.Name.ToUrl();
agencyItem.PublicationDate = DateTime.UtcNow;
It's a null reference exception. Here is a low level debugging stack trace:
Curious enough, if we remove code line "dynamicModuleManager.Lifecycle.Publish(agencyItem, Thread.CurrentThread.CurrentUICulture);", contents are saved but not published. It seems the source of the problem is related to publishing.
Are we doing something wrong? Is there a away to avoid this error? Can you provide some working code to save and publish dynamic content programatically?
We appreciate your help.