Stephen2

Fetch live dynamic items from master ID

Feb 2nd, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. namespace Websilk.Sitefinity {
  2.     public static partial class SitefinityFunctions {
  3.         /* Fetch - ALL */
  4.         private static Type ResolveType(string type) {
  5.             return TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type));
  6.         }
  7.         public static IQueryable<DynamicContent> GetAllDynamicContentItems(string type) {
  8.             return DynamicModuleManager.GetManager().GetDataItems(ResolveType(type));
  9.         }
  10.  
  11.         /* Fetch - LIVE RECORDS */
  12.         public static IQueryable<DynamicContent> GetDynamicContentItems(string type, bool mustBePublished = true) {
  13.             return GetAllDynamicContentItems(type).Where(o => (!mustBePublished || o.Visible) && o.Status == ContentLifecycleStatus.Live);
  14.         }
  15.         public static IQueryable<DynamicContent> GetDynamicContentItems(string type, IEnumerable<Guid> ids, bool mustBePublished = true, bool preserveOrder = false)
  16.         {
  17.             var dynamicContentItems = GetDynamicContentItems(type, mustBePublished).Where(o => ids.Contains(o.Id));
  18.  
  19.             //If you're not asked to preserve the ordering at this point (default), then you can efficiently return a true IQueryable
  20.             if (!preserveOrder)
  21.             {
  22.                 return dynamicContentItems;
  23.             }
  24.  
  25.             //Ordering forces iteration, so has to return a fake IQueryable
  26.             return ids.Select(thisId => dynamicContentItems.SingleOrDefault(o => o.OriginalContentId == thisId)).Where(dynamicContentItem => dynamicContentItem != null).AsQueryable();
  27.         }
  28.  
  29.         /* Fetch - LIVE RECORDS BY MASTERID */
  30.         public static IQueryable<DynamicContent> GetDynamicContentItemsFromMaster(string type, IEnumerable<Guid> ids, bool mustBePublished = true, bool preserveOrder = false) {
  31.             var dynamicContentItems = GetDynamicContentItems(type, mustBePublished).Where(o => ids.Contains(o.OriginalContentId));
  32.  
  33.             //If you're not asked to preserve the ordering at this point (default), then you can efficiently return a true IQueryable
  34.             if (!preserveOrder)
  35.             {
  36.                 return dynamicContentItems;
  37.             }
  38.  
  39.             //Ordering forces iteration, so has to return a fake IQueryable
  40.             return ids.Select(thisId => dynamicContentItems.SingleOrDefault(o => o.OriginalContentId == thisId)).Where(dynamicContentItem => dynamicContentItem != null).AsQueryable();
  41.         }
  42.     }
  43. }
Add Comment
Please, Sign In to add comment