Created
October 17, 2016 21:32
-
-
Save Stephen2/c06227fca18eb4b4489d16e57f2d12fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Fetch - ALL */ | |
private Type ResolveType(string type) { | |
return TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type)); | |
} | |
private IQueryable<DynamicContent> GetAllDynamicContentItems(string dynamicContentTypeName) { | |
var dynamicContentType = ResolveType(dynamicContentTypeName); | |
//Aaaahh non-multisite, how simple you were | |
if (!SystemManager.CurrentContext.IsMultisiteMode) { | |
return DynamicModuleManager.GetManager().GetDataItems(dynamicContentType); | |
} | |
return SitefinityQuery.Get<DynamicContent>(dynamicContentType, DynamicModuleManager.GetManager().Provider) | |
.Where(o => GetDynamicContentApplicationNames(dynamicContentTypeName).Contains(o.ApplicationName)); | |
} | |
/* Fetch - LIVE RECORDS */ | |
public IQueryable<DynamicContent> GetDynamicContentItems(string dynamicContentTypeName) { | |
return GetAllDynamicContentItems(dynamicContentTypeName).Where(o => o.Visible && o.Status == ContentLifecycleStatus.Live); | |
} | |
public DynamicContent GetDynamicContentItem(string dynamicContentTypeName, Guid id) { | |
return GetDynamicContentItems(dynamicContentTypeName).SingleOrDefault(o => o.Id == id); | |
} | |
IEnumerable<string> GetDynamicContentApplicationNames(string dynamicContentTypeName) { | |
//I like to pass around shorthand, but some functions need the full type | |
var fullDynamicContentTypeName = string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", dynamicContentTypeName); | |
//Seems like each type you want to work with has it's own Gotchas, the Gotcha for DynamicContent is that the dataSourceName | |
//Isn't the same as the "Type" and must be looked up via code, unfortunately that code is an Interal Method | |
var moduleBuilderManager = ModuleBuilderManager.GetManager(); | |
var getDynamicModuleTypesMethod = moduleBuilderManager.GetType().GetMethod("GetDynamicModuleTypes", BindingFlags.Instance | BindingFlags.NonPublic); | |
var dynamicModuleTypes = (IQueryable<DynamicModuleType>)getDynamicModuleTypesMethod.Invoke(moduleBuilderManager, null); | |
var dynamicModuleType = dynamicModuleTypes.Single(o => o.TypeNamespace + "." + o.TypeName == fullDynamicContentTypeName); | |
//This has just translated the programattic type name - "MyModuleTitle.ModuleItem" to the Title of the module e.g., "My Module Title" | |
//Back on track with getting provider Names | |
var providerNames = GetProviderNames(dynamicModuleType.ModuleName); | |
//NOW we can't just use these, because the "Default" provider for Sitefinity | |
//Has a different ProviderName from ApplicationName | |
//We could hardcode it, but that's not right, let's look it up in the place Sitefinity sets it. | |
return providerNames.Select(o => DynamicContentProviderApplicationName(o)); | |
} | |
string DynamicContentProviderApplicationName(string providerName) { | |
//Marking this Internal makes code harder and slower too... Should I just hardcode these defaults? No, doesn't feel right to do so. | |
var dynamicModulesConfigType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicModules.Configuration.DynamicModulesConfig"); | |
var dynamicModulesConfig = Config.Get(dynamicModulesConfigType); | |
var dynamicModuleProviders = (ConfigElementDictionary<string, DataProviderSettings>)dynamicModulesConfigType.GetProperty("Providers").GetValue(dynamicModulesConfig, null); | |
return dynamicModuleProviders[providerName].Parameters["applicationName"]; | |
} | |
IEnumerable<string> GetProviderNames(string dataSourceName) { | |
return SystemManager.CurrentContext.CurrentSite.SiteDataSourceLinks | |
.Where(o => o.DataSourceName == dataSourceName) | |
.Select(o => o.ProviderName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment