Skip to content

Instantly share code, notes, and snippets.

@ZNS
Created September 20, 2012 12:35
Show Gist options
  • Save ZNS/3755623 to your computer and use it in GitHub Desktop.
Save ZNS/3755623 to your computer and use it in GitHub Desktop.
Fluent query in EPiServer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Core;
using EPiServer;
using EPiServer.Filters;
using EPiServer.Globalization;
namespace M2B
{
public interface IFindPages
{
IFindPages And();
IFindPages NoAccess();
IFindPages ExcludeShortcuts();
IFindPages ByPageType(int[] pageTypes);
IFindPages ByShortCutType(int shortCutType);
IFindPages ByVisibleInMenu(bool visible);
IFindPages ByAllCategories(CategoryList categories);
IFindPages ByAnyCategory(CategoryList categories);
IFindPages ByProperty(string name, object value, PropertyDataType type);
IFindPages ByProperty(string name, object value, PropertyDataType type, CompareCondition condition);
IFindPages OrderByIndex(bool desc);
IFindPages OrderByDate(bool desc);
IFindPages OrderBy(string property, bool desc);
PageDataCollection Execute();
}
public class FindPages : IFindPages
{
private PageReference _PageLink;
private PropertyCriteriaCollection _Criterias;
private string _Lang = null;
private bool _And = false;
private string _OrderBy = null;
private bool _OrderByDesc = false;
private bool _NoAccess = false;
public FindPages(PageReference pageLink)
{
_PageLink = pageLink;
_Criterias = new PropertyCriteriaCollection();
}
public FindPages(PageReference pageLink, string lang)
{
_PageLink = pageLink;
_Lang = lang;
_Criterias = new PropertyCriteriaCollection();
}
public static IFindPages Query(PageReference pageLink)
{
return new FindPages(pageLink);
}
public static IFindPages Query(PageReference pageLink, string lang)
{
return new FindPages(pageLink, lang);
}
public IFindPages And()
{
_And = true;
return this;
}
public IFindPages NoAccess()
{
_NoAccess = true;
return this;
}
public IFindPages ExcludeShortcuts()
{
_Criterias.Add(new PropertyCriteria()
{
Name = "PageShortcutType",
Type = PropertyDataType.Number,
Condition = EPiServer.Filters.CompareCondition.Equal,
Required = true,
Value = "0"
});
return this;
}
public IFindPages ByPageType(int[] pageTypes)
{
foreach (int i in pageTypes)
{
_Criterias.Add(new PropertyCriteria()
{
Name = "PageTypeID",
Type = PropertyDataType.PageType,
Condition = EPiServer.Filters.CompareCondition.Equal,
Required = _And,
Value = i.ToString()
});
}
_And = false;
return this;
}
public IFindPages ByShortCutType(int shortCutType)
{
_Criterias.Add(new PropertyCriteria()
{
Name = "PageShortcutType",
Type = PropertyDataType.Number,
Condition = EPiServer.Filters.CompareCondition.Equal,
Required = _And,
Value = shortCutType.ToString()
});
_And = false;
return this;
}
public IFindPages ByVisibleInMenu(bool visible)
{
_Criterias.Add(new PropertyCriteria()
{
Name = "PageVisibleInMenu",
Type = PropertyDataType.Boolean,
Condition = EPiServer.Filters.CompareCondition.Equal,
Required = _And,
Value = visible.ToString()
});
_And = false;
return this;
}
public IFindPages ByAnyCategory(CategoryList categories)
{
_Criterias.Add(new PropertyCriteria()
{
Name = "PageCategory",
Type = PropertyDataType.Category,
Condition = EPiServer.Filters.CompareCondition.Contained,
Required = _And,
Value = categories.ToString()
});
_And = false;
return this;
}
public IFindPages ByAllCategories(CategoryList categories)
{
_Criterias.Add(new PropertyCriteria()
{
Name = "PageCategory",
Type = PropertyDataType.Category,
Condition = EPiServer.Filters.CompareCondition.Equal,
Required = _And,
Value = categories.ToString()
});
_And = false;
return this;
}
public IFindPages ByProperty(string name, object value, PropertyDataType type)
{
return ByProperty(name, value, type, CompareCondition.Equal);
}
public IFindPages ByProperty(string name, object value, PropertyDataType type, CompareCondition condition)
{
_Criterias.Add(new PropertyCriteria()
{
Name = name,
Type = type,
Condition = condition,
Required = _And,
Value = value.ToString()
});
_And = false;
return this;
}
public IFindPages OrderByIndex(bool desc)
{
_OrderBy = "PagePeerOrder";
_OrderByDesc = desc;
return this;
}
public IFindPages OrderByDate(bool desc)
{
_OrderBy = "PageStartPublish";
_OrderByDesc = desc;
return this;
}
public IFindPages OrderBy(string property, bool desc)
{
_OrderBy = property;
_OrderByDesc = desc;
return this;
}
public PageDataCollection Execute()
{
PageDataCollection result = null;
if (!_NoAccess)
{
//FindPagesWithCriteria Filters Out Pages User Does Not Have Access To
if (_Lang != null)
result = DataFactory.Instance.FindPagesWithCriteria(_PageLink, _Criterias, _Lang);
else
result = DataFactory.Instance.FindPagesWithCriteria(_PageLink, _Criterias);
}
else
{
if (_Lang != null)
result = DataFactory.Instance.FindAllPagesWithCriteria(_PageLink, _Criterias, _Lang, new LanguageSelector(_Lang));
else
result = DataFactory.Instance.FindAllPagesWithCriteria(_PageLink, _Criterias, ContentLanguage.PreferredCulture.TwoLetterISOLanguageName, new LanguageSelector(ContentLanguage.PreferredCulture.TwoLetterISOLanguageName));
}
if (_OrderBy != null)
{
if (_OrderByDesc)
return new PageDataCollection(result.OrderByDescending(p => p[_OrderBy]));
else
return new PageDataCollection(result.OrderBy(p => p[_OrderBy]));
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment