Hi everyone,

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;
dynamicModuleManager.Lifecycle.Publish(agencyItem, Thread.CurrentThread.CurrentUICulture);

dynamicContents.Add(agencyItem);
}

TransactionManager.FlushTransaction(transactionName);
TransactionManager.CommitTransaction(transactionName);

(...)

It's a null reference exception. Here is a low level debugging stack trace:

5cd8e2a4 456fbb1d Telerik.Sitefinity.DynamicModules.DynamicModuleDataProvider.ApplyFilters(Telerik.Sitefinity.Model.IDataItem)
5cd8e2fc 456fba96 DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.<ApplyFilters_DelegateImplementation>__57(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e328 3a8af8ad Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior+c__DisplayClass1.b__0(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextHandlerDelegate)
5cd8e350 3a8af801 Telerik.Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeHandlerDelegate)
5cd8e368 3a8af634 Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e380 3a8af5a6 Telerik.Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeInterceptionBehaviorDelegate)
5cd8e398 456fba26 DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.ApplyFilters(Telerik.Sitefinity.Model.IDataItem)
5cd8e3b0 456fb733 Telerik.Sitefinity.DynamicModules.Model.DynamicContentUrlData.get_Parent()
5cd8e3c0 456fb63d Telerik.Sitefinity.Modules.GenericContent.UrlDataProviderBase.ConfigureUrl(Telerik.Sitefinity.GenericContent.Model.UrlData, Boolean, Boolean)
5cd8e3dc 456efe05 Telerik.Sitefinity.Modules.GenericContent.UrlDataProviderBase.UpdateUrlInSpecificCulture[[System.__Canon, mscorlib]](System.__Canon, System.Globalization.CultureInfo, System.String, Boolean, Boolean)
5cd8e40c 456ee6fc Telerik.Sitefinity.Modules.GenericContent.UrlDataProviderBase.AddItemUrlInternal[[System.__Canon, mscorlib]](System.__Canon, System.Func`3<System.__Canon,System.Globalization.CultureInfo,System.String>, Boolean, Boolean)
5cd8e474 456ee0d2 Telerik.Sitefinity.Modules.GenericContent.UrlDataProviderBase.RecompileItemUrls[[System.__Canon, mscorlib]](System.__Canon)
5cd8e49c 456edfbe DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.<RecompileItemUrls_DelegateImplementation>__61[[System.__Canon, mscorlib]](Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e4d4 3a8af8ad Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior+c__DisplayClass1.b__0(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextHandlerDelegate)
5cd8e4fc 3a8af801 Telerik.Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeHandlerDelegate)
5cd8e514 3a8af634 Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e52c 3a8af5a6 Telerik.Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeInterceptionBehaviorDelegate)
5cd8e544 456edf0d DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.RecompileItemUrls[[System.__Canon, mscorlib]](System.__Canon)
5cd8e56c 3e7abde8 Telerik.Sitefinity.DynamicModules.DynamicModuleManager.Provider_Executing(System.Object, Telerik.Sitefinity.Data.ExecutingEventArgs)
5cd8e6d8 3a93edaa Telerik.Sitefinity.Data.ManagerBase`1[[System.__Canon, mscorlib]].Provider_Executing(System.Object, Telerik.Sitefinity.Data.ExecutingEventArgs)
5cd8e6f0 3a93ed34 Telerik.Sitefinity.Data.DataProviderBase.OnExecuting(Telerik.Sitefinity.Data.ExecutingEventArgs)
5cd8e6fc 3e7ab763 DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.<OnExecuting_DelegateImplementation>__117(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e728 3a8af8ad Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior+c__DisplayClass1.b__0(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextHandlerDelegate)
5cd8e750 3a8af801 Telerik.Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeHandlerDelegate)
5cd8e768 3a8af634 Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e780 3a8af5a6 Telerik.Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeInterceptionBehaviorDelegate)
5cd8e798 3e7ab6e6 DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.OnExecuting(Telerik.Sitefinity.Data.ExecutingEventArgs)
5cd8e7b0 3a93e21f Telerik.Sitefinity.Data.DataEventsCallHandler.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextHandlerDelegate)
5cd8e7d0 3a93e034 Telerik.Sitefinity.Security.SitefinityAuthorizationCallHandler.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextHandlerDelegate)
5cd8e7fc 3a8af856 Telerik.Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeHandlerDelegate)
5cd8e814 3a8af634 Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.GetNextInterceptionBehaviorDelegate)
5cd8e82c 3a8af5a6 Telerik.Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorPipeline.Invoke(Telerik.Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation, Telerik.Microsoft.Practices.Unity.InterceptionExtension.InvokeInterceptionBehaviorDelegate)
5cd8e844 456f9789 DynamicModule.ns.Wrapped_OpenAccessDynamicModuleProvider_6379bac6489b474290e62eb49a345707.FlushTransaction()
5cd8e858 456f970e Telerik.Sitefinity.Data.TransactionManager.b__d(Wrapper)
5cd8e860 3ef5eae5 Telerik.Sitefinity.Data.TransactionManager.DoWithConnections(Connections, System.Action`1, System.Collections.Generic.IEnumerable`1, Int32)
5cd8e8b8 456f1194 Telerik.Sitefinity.Data.TransactionManager.FlushTransaction(System.String)
5cd8e8c8 3e7ab0c2 Va.Cms.Dal.Services.Agencies.AgencyDynamicContentService.CommitAgencies(Va.Cms.Mdl.MapItems.Agency[])
5cd8ea34 3e7aaa02 Va.Cms.Dal.Services.Agencies.AgencyDynamicContentService.AddOrUpdate(System.Collections.Generic.IReadOnlyDictionary`2<System.Globalization.CultureInfo,System.Collections.Generic.IEnumerable`1<Va.Cms.Mdl.MapItems.Agency>>)
5cd8eae4 2e33ec8e Va.Cms.Dal.Services.Agencies.AgencyImportService+d__3.MoveNext()
5cd8eb64 637527f9 System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(System.Object)
5cd8eb6c 637740c5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
5cd8ebd8 63773fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
5cd8ebec 6375275b System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
5cd8ec1c 637d9e8b System.Threading.Tasks.AwaitTaskContinuation.InvokeAction(System.Object)
5cd8ec20 637d6655 System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback, System.Object, System.Threading.Tasks.Task ByRef)
5cd8ec58 637d65e9 System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(System.Threading.Tasks.Task, Boolean)
5cd8ec74 6373f5b7 System.Threading.Tasks.Task.FinishContinuations()
5cd8ecdc 6373f109 System.Threading.Tasks.Task.FinishStageThree()
5cd8ece8 6373f081 System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].TrySetResult(System.__Canon)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.__Canon, mscorlib]].SetResult(System.__Canon)
Va.Cms.Dal.Services.Agencies.AgencyService+d__5.MoveNext()
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(System.Object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
System.Threading.Tasks.AwaitTaskContinuation.InvokeAction(System.Object)
System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback, System.Object, System.Threading.Tasks.Task ByRef)
System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(System.Threading.Tasks.Task, Boolean)
System.Threading.Tasks.Task.FinishContinuations()
System.Threading.Tasks.Task.FinishStageThree()
System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].TrySetResult(System.__Canon)
System.Threading.Tasks.Task+WhenAllPromise`1[[System.__Canon, mscorlib]].Invoke(System.Threading.Tasks.Task)
System.Threading.Tasks.Task.FinishContinuations()
System.Threading.Tasks.Task.FinishStageThree()
System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].TrySetResult(System.__Canon)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.__Canon, mscorlib]].SetResult(System.__Canon)
Va.Cms.Dal.Services.Agencies.AgencyService+d__6.MoveNext()
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(System.Object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
System.Threading.Tasks.AwaitTaskContinuation.InvokeAction(System.Object)
System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback, System.Object, System.Threading.Tasks.Task ByRef)
System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(System.Threading.Tasks.Task, Boolean)
System.Threading.Tasks.Task.FinishContinuations()
System.Threading.Tasks.Task.FinishStageThree()
System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].TrySetResult(System.__Canon)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.__Canon, mscorlib]].SetResult(System.__Canon)
Va.XCut.Back.Client.ApiClient+d__21`1[[System.__Canon, mscorlib]].MoveNext()
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(System.Object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
System.Threading.Tasks.AwaitTaskContinuation.InvokeAction(System.Object)
System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback, System.Object, System.Threading.Tasks.Task ByRef)
System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(System.Threading.Tasks.Task, Boolean)
System.Threading.Tasks.Task.FinishContinuations()
System.Threading.Tasks.Task.FinishStageThree()
System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].TrySetResult(System.__Canon)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.__Canon, mscorlib]].SetResult(System.__Canon)
Va.XCut.Back.Client.ApiClient+d__26.MoveNext()
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(System.Object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation+c.<.cctor>b__8_0(System.Object)
Va.Cms.Utils.Helpers.CultureAwareSynchronizationContext+c__DisplayClass6_0.b__0(System.Object)
System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
System.Threading.ThreadPoolWorkQueue.Dispatch()
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
[DebuggerU2MCatchHandlerFrame: 5cd8fa08] 
[ContextTransitionFrame: 5cd8fa74] 
[DebuggerU2MCatchHandlerFrame: 5cd8fc00]

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.