Tag Archives: episerver cms

EPiServer:PageList with SelectedItemTemplate

This is a quick guide how to extend EPiServer:PageList with a SelectedItemTemplate. This is a good start if you want to try to extend it with a SeparatorTemplate or something more spectacular.

1. Create a class
I.e. PageList.cs, a recommendation is to follow EPiServers namespace, so in this case I would create my class in Antecknat.Web.WebControls.
This class should inherit EPiServer.Web.WebControls.PageList

namespace Antecknat.Web.WebControls
{
public class PageList : EPiServer.Web.WebControls.PageList
{

2. Create the SelectedItemTemplate
This is done by the following code:

private ITemplate _selectedItemTemplate;
[TemplateContainer(typeof(PageTemplateContainer)), Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate SelectedItemTemplate
{
get
{
return this._selectedItemTemplate;
}
set
{
this._selectedItemTemplate = value;
}
}

3. Modify CreateChildControls()
With Reflector (the best program ever made?) we can reuse the way controls are added to the regular PageList. We need to change the current method so that the SelectedItemTemplate is used when is supposed to.
In this case I want the current page, the parent page, the parents parents page and so on to count as selected. So we’ll make a PageReferenceCollection containing these pages.

// Find pages that should be marked as Selected
PageReferenceCollection prcActivePages = new PageReferenceCollection();
PageReference prCurrentLevel = CurrentPage.PageLink;
while (prCurrentLevel != null && prCurrentLevel != PageReference.EmptyReference)
{
prcActivePages.Add(prCurrentLevel);
prCurrentLevel = EPiServer.DataFactory.Instance.GetPage(prCurrentLevel).ParentLink;
}

And where the regular ItemTemplate is added, we change the code from:

ItemTemplate.InstantiateIn(control2);

to:

if (prcActivePages.Contains(pages[i].PageLink)) // Add to SelectedItemTemplate if in Selected collection
SelectedItemTemplate.InstantiateIn(control2);
else // Add to normal ItemTemplate
ItemTemplate.InstantiateIn(control2);

4. Change web.config
This is not a mandatory step, but if you always want to be able to use your fresh PageList from any aspx or ascx in the project, add teh following line in <system.web><pages><controls>

<add tagPrefix="Antecknat" namespace="Antecknat.Web.WebControls" assembly="Antecknat"/>

5. Use your PageList
Now you can use your PageList in your .aspx and .ascx files.
Just type

<Antecknat:PageList ID="PageList1" runat="server>
<HeaderTemplate><ul></HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate><li><%# Container.CurrentPage.PageName %></li>
<SelectedItemTemplate><li class="selected"><%# Container.CurrentPage.PageName %></li>
</Antecknat:PageList>

And then add a PageDataCollection from code behind and databind it.

Download sourcecode
PageList.zip

Using XForms to store information

At the moment I’m adding some functionallity to vinsprit.se, so that a visitor can add products, that aren’t in the regular assortment, to a cart and by a simple click send this cart by fax to the closest Systembolag. A pretty nice feature. The customer also wanted to store the orderinfo in episerver if something goes wrong, so I thought that you must be able to do that with XForms. And yes, it was very simple.

First of all I added a “XForms form”-property to the pagetype. Then I made an empty Form. The rest is handled from code behind. The following example works both with 4.6 and CMS 5. Hopefully in CMS 5 R2 aswell.. 🙂

// Get guid for the form and creates instance
XForm form = XForm.CreateInstance(new Guid((String)CurrentPage["XFormPropertyName"]));
// This is where data is stored
XFormData form_data = form.CreateFormData();
//We want this info related to the order page
form_data.PageId = CurrentPage.PageLink.ID;
// We want it saved in the database
form_data.ChannelOptions = ChannelOptions.Database;

// Here we add the values.
form_data.SetValue("Key 1", "some value");
form_data.SetValue("Key 2", "some other value");
// and saves it
form_data.Send();

Pretty nice if you want to avoid creating your own sql-table or some other way to store data.

MultiPage Property to EPiServer 5

I know there is a great MultiPage Property on EPiCode, but sometimes I want to use a bit more simple MultiPage picker, and not be able to choose to link documents and other pages etc. So when I had some spare time a few weeks ago, I wrote my own multipage property.

To make the property as powerful as possible I’ve added the oppertunity to specify the startnode when you add a page, and there is also a possibility to choose wich PageTypes you are allowed to use. If you leave the help-text blank you will be able to add all pagetypes and the startnode will be the start page. Defining these values in help-text may not be the best way to go, but it’s the easiest way if you want it as dynamically as possible. I usually change where to define these values in my projects, etc a settings page or web.config, but in this case the help text is wonderful. 🙂

If the editor tries to add an invalid page type, the following msg will appear.

To get the selected pages when you code, just use this line

PageDataCollection pdc = Obg.SpecializedProperties.Util.MultiPage.GetPageDataCollectionByPropertyValue(CurrentPage["MultiPageContacts"]);

You can download the file here: MultiPage Property (binaries)

In this dll, my DropDown Property is also added.

Feel free to give me feedback on these properties or you want the source code etc.