In this tutorial, I will show you how to quickly create an EPi server admin plug-in from scratch... in less than 5 minutes, that will also load and save data into Epi-server. The plug-in will display the current time and a button to update it. The time will be stored within epi-server so it will persist. After we create the plug-in you will be able to see it in the Admin section for your website, available within the Admin Mode -> Tools menu (as seen below):
Create the aspx page that will display the plugin. In Visual Studio, create a new standard Page type. You will need to decide where your admin plug-in is going to live in your webroot. For this example, I am going to add them into a folder in:
Webroot -> Templates -> Plugins
As the actual location doesn't matter but for keeping your project easy to maintain, I would recommend putting it somewhere within templates under your site folder. For my page, I have called the page 'Example' with a matching namespace. My aspx file is very simple. All I'm doing is adding a button and a call to a method to display the time :
The time is <%= GetTheTime() %>
The code behind is a bit more tricky as it has to do several things :
- Define the control as an Epi plug-in
- Define GetTheTime to display the time
- Define UpdateTheTime to get time
- Store everything within Episerver so the time persists.
Within the code-behind, the first thing we need to do is change the base type of the control from the .NET 'Page' to an EPI 'SimplePage'. Add the Episerver plug-in declaration attribute so it displays in the back-end.
This is a single line that you need to place on top of the class declaration. Call it whatever you want but make sure the Url property points to the aspx page in your webroot.
The next step is to create a method to load and store the time into Epi. This data access is done via the Epi API using the PlugInSettings with the Populate and Save method. The only downside of using 'PlugInSettings' is it works using DataSets, which isn't ideal.
To keep things simple, I've wrapped these calls into a custom class that my page will call. In the constructor of the class, I initialize a new Dataset on creation and stores or loads a string.
One thing to note is that I'm using Log4net out the Epi box on all EPi sites for error handling. You can see in Load and Save that I'm using 'PlugInSettings' to store or load the data. Now the only bit left to do is wire up the new class in our control.
It really is that simple. You can download the full source code here from my Github Episerver Goodies repository available from here :)