I've got a dynamic module that I created with some data that gets imported from an import tool I've created. Now I keep the config information for these modules stored under /app_data/Sitefinity and check them into our tfs version control system and everything works great under our local and dev environments. But we had an issue on QA where the build didn't import the module correctly and to fix this I eventually had to delete the module on QA and imported it back in from the config we had on dev. So now the dynamic module works great from our administration module and we can manually create the data there fine.
The issue now is that when I run my import tool, it acts and runs fine against our QA site, but when we go to look into the data on the administration panel it doesn't show up on the site. I've also checked the table in sql server and the imported data is in there. Also I've made sure that the data is published and set to visible, so I'm not sure why the data isn't showing up in the administration section. Anybody have any ideas??
Here's the code:
// Set the culture name for the multilingual fields
var cultureName = "en";
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
Type locationType =
TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Locations.Location");
DynamicContent location = null;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager("dynamicProvider2");
dynamicModuleManager.Provider.SuppressSecurityChecks = true;
location = dynamicModuleManager.GetDataItems(locationType)
.FirstOrDefault(
x =>
x.GetValue<string>("OperationId") == importLocation.OperationId.ToString() &&
x.Status == ContentLifecycleStatus.Master && x.Visible == true);
if (location == null && importLocation.IsActive)
{
// We have a new location.
DynamicContent locationItem = dynamicModuleManager.CreateDataItem(locationType);
locationItem.SetString("Title", importLocation.Title, cultureName);
locationItem.SetString("Description", importLocation.Description, cultureName);
locationItem.SetString("OperationId", importLocation.OperationId.ToString(), cultureName);
Address address = new Address();
CountryElement addressCountry =
Config.Get<LocationsConfig>().Countries.Values.First(x => x.Name == "United States");
address.CountryCode = addressCountry.IsoCode;
address.StateCode = importLocation.State;
address.City = importLocation.City;
address.Street = importLocation.Street;
address.Zip = importLocation.Zip;
address.Latitude = importLocation.Latitude;
address.Longitude = importLocation.Longitude;
address.MapZoomLevel = 8;
locationItem.SetValue("Address", address);
locationItem.Visible = true;
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
taxonomyManager.Provider.SuppressSecurityChecks = true;
var foundRootServiceArea = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>()
.FirstOrDefault(t => t.Name == "Service-Areas");
foreach (var serviceArea in importLocation.ServiceAreas)
{
var foundServiceArea =
foundRootServiceArea.Taxa.FirstOrDefault(w => w.Name == serviceArea);
if (foundServiceArea != null)
{
locationItem.Organizer.AddTaxa("ServiceAreas", foundServiceArea.Id);
}
else
{
var newServiceArea = taxonomyManager.CreateTaxon<HierarchicalTaxon>();
newServiceArea.Title = serviceArea;
newServiceArea.Name = serviceArea;
foundRootServiceArea.Taxa.Add(newServiceArea);
locationItem.Organizer.AddTaxa("ServiceAreas", newServiceArea.Id);
}
}
locationItem.SetValue("PublicationDate", DateTime.UtcNow);
// Modified to publish instead of set items as draft
locationItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published",
new CultureInfo(cultureName));
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();
// Use lifTecycle so that LanguageData and other Multilingual related values are correctly created
DynamicContent checkOutLocationItem =
dynamicModuleManager.Lifecycle.CheckOut(locationItem) as DynamicContent;
dynamicModuleManager.Lifecycle.CheckIn(checkOutLocationItem);
dynamicModuleManager.SaveChanges();
return Ok();
}
else if (location != null)
{
// Check to see if we need to update each field.
if (location.DoesFieldExist("Title") && !String.IsNullOrEmpty(location.GetValue("Title").ToString()))
{
if (location.GetValue("Title").ToString() != importLocation.Title)
{
location.SetString("Title", importLocation.Title);
}
}
if (location.DoesFieldExist("Description") &&
!String.IsNullOrEmpty(location.GetValue("Description").ToString()))
{
if (location.GetValue("Description").ToString() != importLocation.Description)
{
location.SetString("Description", importLocation.Description);
}
}
if (location.DoesFieldExist("Address"))
{
var address = location.GetValue<Address>("Address");
if (address.City != importLocation.City)
{
address.City = importLocation.City;
}
if (address.StateCode != importLocation.State)
{
address.StateCode = importLocation.State;
}
if (address.Street != importLocation.Street)
{
address.Street = importLocation.Street;
}
if (address.Zip != importLocation.Zip)
{
address.Zip = importLocation.Zip;
}
if (address.Latitude != importLocation.Latitude)
{
address.Latitude = importLocation.Latitude;
}
if (address.Longitude != importLocation.Longitude)
{
address.Longitude = importLocation.Longitude;
}
location.SetValue("Address", address);
}
location.Visible = importLocation.IsActive;
if (!importLocation.IsActive)
{
location.Status = ContentLifecycleStatus.Deleted;
dynamicModuleManager.SaveChanges();
}
else
{
location.SetWorkflowStatus(
dynamicModuleManager.Provider.ApplicationName,
"Published", new CultureInfo(cultureName));
dynamicModuleManager.SaveChanges();
DynamicContent checkOutLocationItem =
dynamicModuleManager.Lifecycle.CheckOut(location) as
DynamicContent;
dynamicModuleManager.Lifecycle.CheckIn(checkOutLocationItem);
dynamicModuleManager.SaveChanges();
}
return Ok();
}
In order to retrieve the provider dynamically for the current site based on the module name, I have created this helper method.
public static DynamicModuleManager GetDynamicProvider(string moduleName)
{
// Set the provider name for the DynamicModuleManager here. All available providers are listed in
// Administration -> Settings -> Advanced -> DynamicModules -> Providers
var provider = SystemManager.CurrentContext.CurrentSite.GetDefaultProvider(moduleName);
var providerName = (provider != null) ? provider.ProviderName : DynamicModuleManager.GetDefaultProviderName();
return DynamicModuleManager.GetManager(providerName);
}