If you’re working with Dynamic Modules in Sitefinity and creating your own MVC based custom widgets for these modules then you may be facing a problem where when you use the API to create a child Dynamic Content item for a parent item you might not be able to extract them with the API, or you simply get a Count of zero.

I recently ran into this problem and no matter how much I followed the Code Reference provided by Sitefinity’s Module Builder I just couldn’t get to the point of extracting the records that I had created.

In my scenario I had a parent Content Type of Fund (financial) and a child content type of Historical Record. Each historical record represents a single days trading prices for a particular Fund. The Fund is managed in the Sitefinity easily enough, but the Historical Records are imported via XML feeds provided by a third party.

Following the basic guidance of the Module Builder I imported each Historical Record as follows:

var dynamicModuleManager = new DynamicModuleManager(String.Empty);
Type historicDataType = TypeResolutionService.ResolveType(“Telerik.Sitefinity.DynamicTypes.Model.ExchangeTradedFunds.HistoricDataEntry”);
DynamicContent historicDataEntry = dynamicModuleManager.CreateDataItem(historicDataType);

//set the values for the entry
historicDataEntry.SetValue(“Title”, selectedFundText + ” ” + date.ToString(“yyyy-MM-dd”));
historicDataEntry.SetValue(“Date”, date);
historicDataEntry.SetValue(“AUM”, aum);
historicDataEntry.SetValue(“NAV”, nav);

//set additional related information
historicDataEntry.SetString(“UrlName”, selectedFundTextFixed + “-” + date.ToString(“yyyy-MM-dd”));
historicDataEntry.SetValue(“Owner”, SecurityManager.GetCurrentUserId());
historicDataEntry.SetValue(“PublicationDate”, DateTime.Now);

//set status
dynamicModuleManager.Lifecycle.Publish(historicDataEntry);
historicDataEntry.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, “Published”);

//set the parent id
Type etfType = TypeResolutionService.ResolveType(“Telerik.Sitefinity.DynamicTypes.Model.ExchangeTradedFunds.ExchangeTradedFund”);
historicDataEntry.SetParent(GetFundIDToUse(), etfType.FullName);

//save the item
dynamicModuleManager.SaveChanges();

 

It turns out that the guidance provided in the Module Builder is either wrong or the example is incomplete or misleading.

You can see the effect in the sf_dynamic_content table where it adds two records for each Historic Record, which by the way is correct. The first record is the master and the second is the live copy. The problem you’ll notice with this approach is that for the Live record the system_parent_id value is null.

The solution to the problem is to set the Parent before publishing and setting the workflow status as the example below shows:

var dynamicModuleManager = new DynamicModuleManager(String.Empty);
Type historicDataType = TypeResolutionService.ResolveType(“Telerik.Sitefinity.DynamicTypes.Model.ExchangeTradedFunds.HistoricDataEntry”);
DynamicContent historicDataEntry = dynamicModuleManager.CreateDataItem(historicDataType);

//set the values for the entry
historicDataEntry.SetValue(“Title”, selectedFundText + ” ” + date.ToString(“yyyy-MM-dd”));
historicDataEntry.SetValue(“Date”, date);
historicDataEntry.SetValue(“AUM”, aum);
historicDataEntry.SetValue(“NAV”, nav);

//set additional related information
historicDataEntry.SetString(“UrlName”, selectedFundTextFixed + “-” + date.ToString(“yyyy-MM-dd”));
historicDataEntry.SetValue(“Owner”, SecurityManager.GetCurrentUserId());
historicDataEntry.SetValue(“PublicationDate”, DateTime.Now);

//set the parent id
Type etfType = TypeResolutionService.ResolveType(“Telerik.Sitefinity.DynamicTypes.Model.ExchangeTradedFunds.ExchangeTradedFund”);
historicDataEntry.SetParent(GetFundIDToUse(), etfType.FullName);

//set status
dynamicModuleManager.Lifecycle.Publish(historicDataEntry);
historicDataEntry.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, “Published”);

//save the item
dynamicModuleManager.SaveChanges();

I hope this helps someone else save some time.