Displaying plugins dynamically
I recently found the need to display a plugin dynamically, in a very similar way to the ‘Form Data’ plugin, in a certain PropertyData type existed within a page type. After reflecting the code for the form data plugin I was able to duplicate the functionality:
Firstly the plugin class needs to inherit from ICustomPlugInLoader, which exposes the method List()
public PlugInDescriptor[] List()
{
if (PageAllowsComments())
{
// Show plugin if comments allowed
PlugInDescriptor descriptor = PlugInDescriptor.Load(GetType());
return new PlugInDescriptor[] { descriptor };
}
// Hide plugin if comments not allowed
return new PlugInDescriptor[0];
}
private bool PageAllowsComments()
{
PageData page = GetCurrentPage();
if (page != null)
{
foreach (PropertyData propertyData in page.Property)
{
if (propertyData is CommentsXForm) return true;
}
}
return false;
}
private PageData GetCurrentPage()
{
PageData currentPage;
if (PageReference.IsNullOrEmpty(base.CurrentPage.PageLink))
{
return null;
}
try
{
currentPage = base.CurrentPage;
}
catch (EPiServerException)
{
return null;
}
return currentPage;
}
Thanks for this - I was looking around for a way to be able to load edit panel plug-ins on the fly depending on the base page type.
/ Ben Morris