Skip to content

Instantly share code, notes, and snippets.

@khurramkhang
Created December 11, 2018 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khurramkhang/6c267ccb76d1c552ee3c6cee219f32a6 to your computer and use it in GitHub Desktop.
Save khurramkhang/6c267ccb76d1c552ee3c6cee219f32a6 to your computer and use it in GitHub Desktop.
Get Specific Epi Server Form Elements
using EPiServer.Core;
using EPiServer.Forms.Core;
using EPiServer.Forms.Core.Models;
using EPiServer.Forms.Helpers.Internal;
using EPiServer.Forms.Implementation.Elements;
using System.Collections.Generic;
using System.Linq;
namespace PixieDigital.EpiServer.Extensions
{
public static class FormExtensions
{
public static IEnumerable<T> GetSpecificFormElements<T>(this FormIdentity formIdentity, bool filteredItemsOnly = true) where T : ElementBlockBase
{
return GetSpecificFormElements<T>(formIdentity.GetFormBlock(), filteredItemsOnly);
}
public static IEnumerable<T> GetSpecificFormElements<T>(this FormContainerBlock formContainerBlock, bool filteredItemsOnly = true) where T : ElementBlockBase
{
if (formContainerBlock != null && formContainerBlock.ElementsArea != null && formContainerBlock.ElementsArea.Items != null && formContainerBlock.ElementsArea.Items.Count() != 0)
{
var formElements = filteredItemsOnly ? formContainerBlock.ElementsArea.FilteredItems : formContainerBlock.ElementsArea.Items;
foreach (var item in formElements)
{
T element = item.ContentLink.GetContent((formContainerBlock as ILocale).Language.Name) as T;
if (element != null)
{
yield return element;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment