Advertisement
Stephen2

SitefinityQuery - DynamicContent

Mar 1st, 2014
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. /* Fetch - ALL */
  2. private Type ResolveType(string type) {
  3.     return TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type));
  4. }
  5. private IQueryable<DynamicContent> GetAllDynamicContentItems(string dynamicContentTypeName) {
  6.     var dynamicContentType = ResolveType(dynamicContentTypeName);
  7.  
  8.     //Aaaahh non-multisite, how simple you were
  9.     if (!SystemManager.CurrentContext.IsMultisiteMode) {
  10.         return DynamicModuleManager.GetManager().GetDataItems(dynamicContentType);
  11.     }
  12.  
  13.     return SitefinityQuery.Get<DynamicContent>(dynamicContentType, DynamicModuleManager.GetManager().Provider)
  14.         .Where(o => GetDynamicContentApplicationNames(dynamicContentTypeName).Contains(o.ApplicationName));
  15. }
  16.  
  17. /* Fetch - LIVE RECORDS */
  18. public IQueryable<DynamicContent> GetDynamicContentItems(string dynamicContentTypeName) {
  19.     return GetAllDynamicContentItems(dynamicContentTypeName).Where(o => o.Visible && o.Status == ContentLifecycleStatus.Live);
  20. }
  21. public DynamicContent GetDynamicContentItem(string dynamicContentTypeName, Guid id) {
  22.     return GetDynamicContentItems(dynamicContentTypeName).SingleOrDefault(o => o.Id == id);
  23. }
  24.  
  25. IEnumerable<string> GetDynamicContentApplicationNames(string dynamicContentTypeName) {
  26.     //I like to pass around shorthand, but some functions need the full type
  27.     var fullDynamicContentTypeName = string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", dynamicContentTypeName);
  28.  
  29.     //Seems like each type you want to work with has it's own Gotchas, the Gotcha for DynamicContent is that the dataSourceName
  30.     //Isn't the same as the "Type" and must be looked up via code, unfortunately that code is an Interal Method
  31.     var moduleBuilderManager = ModuleBuilderManager.GetManager();
  32.     var getDynamicModuleTypesMethod = moduleBuilderManager.GetType().GetMethod("GetDynamicModuleTypes", BindingFlags.Instance | BindingFlags.NonPublic);
  33.     var dynamicModuleTypes = (IQueryable<DynamicModuleType>)getDynamicModuleTypesMethod.Invoke(moduleBuilderManager, null);
  34.     var dynamicModuleType = dynamicModuleTypes.Single(o => o.TypeNamespace + "." + o.TypeName == fullDynamicContentTypeName);
  35.     //This has just translated the programattic type name - "MyModuleTitle.ModuleItem" to the Title of the module e.g., "My Module Title"
  36.  
  37.     //Back on track with getting provider Names
  38.     var providerNames = GetProviderNames(dynamicModuleType.ModuleName);
  39.  
  40.     //NOW we can't just use these, because the "Default" provider for Sitefinity
  41.     //Has a different ProviderName from ApplicationName
  42.     //We could hardcode it, but that's not right, let's look it up in the place Sitefinity sets it.
  43.     return providerNames.Select(o => DynamicContentProviderApplicationName(o));
  44. }
  45.  
  46. string DynamicContentProviderApplicationName(string providerName) {
  47.     //Marking this Internal makes code harder and slower too...  Should I just hardcode these defaults?  No, doesn't feel right to do so.
  48.     var dynamicModulesConfigType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicModules.Configuration.DynamicModulesConfig");
  49.     var dynamicModulesConfig = Config.Get(dynamicModulesConfigType);
  50.     var dynamicModuleProviders = (ConfigElementDictionary<string, DataProviderSettings>)dynamicModulesConfigType.GetProperty("Providers").GetValue(dynamicModulesConfig, null);
  51.     return dynamicModuleProviders[providerName].Parameters["applicationName"];          
  52. }
  53.  
  54. IEnumerable<string> GetProviderNames(string dataSourceName) {
  55.     return SystemManager.CurrentContext.CurrentSite.SiteDataSourceLinks
  56.         .Where(o => o.DataSourceName == dataSourceName)
  57.         .Select(o => o.ProviderName);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement