Overview

Another example of using the API to help with development.  This time reordering many properties quickly.

Process and caveats

In EPiServer 4.6, this process doesn't work quite as well as it should.  This can result in interesting behaviour of properties not displaying in the editor in the same order as in the administration of the Page Type.  Also, some changes don't seem to get saved.  I've done my best to get round these issues, but if you've got any suggestions, please post the comments.

So, how do I do it?  As before, I'm hosting the code on a very simple ASP.NET page.  It uses the EPiServer APIs to find the PageDefinition objects associated with a PageType.  Then it reorders them as requested in the UI.  I don't remove the PageDefinitions from the Definitions collection, because I don't know what that will do to pages with the property!  That means I have to be a little clever about how I reorder them.

Helpfully, EPiServer provides MoveUp() and MoveDown() methods on a PageDefinition.  Unhelpfully, these don't seem particularly reliable!  To make sure they work, I do exactly one movement at a time, and in a carefully ordered way.  This ensures that early changes do not change the behaviour of later changes.  This is one area that can catch you out!

Example code

ReorderPageTypeProperties.aspx

    ReorderPageTypeProperties.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReorderPageTypeProperties.aspx.cs" Inherits="EPiServerSample.cms.admin.ReorderPageTypeProperties" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        label { font-weight: bold; color: navy; }
        select { margin-top: 0.5em; margin-bottom: 1em;}
        label.property { float:left; width: 150px; }
        input.property { width: 200px; }
    </style>
</head>
<body>
    <h1>EPiServer:  Reorder Page Type Properties</h1>
    <form id="form1" runat="server">
    <div style="width:40%; float:left; margin-right:2em;">
      <asp:Label runat="server" AssociatedControlID="lbSourcePageType">Page Type:</asp:Label><br />
      <asp:DropDownList Width="100%" ID="lbSourcePageType" AutoPostBack="true" runat="server" /><br />
    </div>
    <div style="clear:both;">&nbsp;</div>
    <div style="width:40%; float:left; margin-right: 2em;">
      <asp:Label ID="Label1" runat="server" AssociatedControlID="lbSourceProperties">Template Property:</asp:Label><br />
      <asp:ListBox Width="100%" Height="400" ID="lbSourceProperties" SelectionMode="Multiple" AutoPostBack="false" runat="server" />
    </div>
    <div style="width:40%; float:left;">
        <br /><br />
        
        <asp:Button ID="btnMoveUpPage" Text="Move up 5" runat="server"
            OnClick="btnMoveUpPage_Click" /><br /><br />
        <asp:Button ID="btnMoveUp2" Text="Move up 2" runat="server"
            OnClick="btnMoveUp2_Click" /><br /><br />
        <asp:Button ID="btnMoveUp" Text="Move up" runat="server"
            OnClick="btnMoveUp_Click" /><br /><br />
        <asp:Button ID="btnMoveDown" Text="Move down" runat="server"
            OnClick="btnMoveDown_Click" /><br /><br />
        <asp:Button ID="btnMoveDown2" Text="Move down 2" runat="server"
            OnClick="btnMoveDown2_Click" /><br /><br />
        <asp:Button ID="btnMoveDownPage" Text="Move down 5" runat="server"
            OnClick="btnMoveDownPage_Click" />
            
    </div>
    </form>
</body>
</html>

ReorderPageTypeProperties.aspx

    ReorderPageTypeProperties.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace EPiServerSample.cms.admin
{
    public partial class ReorderPageTypeProperties : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EPiServer.DataAbstraction.PageTypeCollection p =
                    EPiServer.DataAbstraction.PageType.List();

                lbSourcePageType.DataSource = p;
                lbSourcePageType.DataValueField = "ID";
                lbSourcePageType.DataTextField = "Name";
                lbSourcePageType.DataBind();
            }

            lbSourcePageType.SelectedIndexChanged +=
                new EventHandler(this.lbPageType_SelectedIndexChanged);

        }
        
        protected void lbPageType_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList lb = (DropDownList)sender;
            ListBox lbp = null;

            if (object.ReferenceEquals(lb, this.lbSourcePageType)) lbp = lbSourceProperties;

            lbp.Items.Clear();

            if (!String.IsNullOrEmpty(lb.SelectedValue))
            {
                EPiServer.DataAbstraction.PageType pt = EPiServer.DataAbstraction.PageType.Load(int.Parse(lb.SelectedValue));
                lbp.DataSource = pt.Definitions;
                lbp.DataTextField = "Name";
                lbp.DataValueField = "ID";
                lbp.DataBind();
            }
        }

        protected void btnMoveUpPage_Click(object source, EventArgs e)
        {
            Reorder(true, 5);
        }
        protected void btnMoveDownPage_Click(object source, EventArgs e)
        {
            Reorder(false, 5);
        }

        protected void btnMoveUp2_Click(object source, EventArgs e)
        {
            Reorder(true, 2);
        }
        protected void btnMoveDown2_Click(object source, EventArgs e)
        {
            Reorder(false, 2);
        }

        protected void btnMoveUp_Click(object source, EventArgs e)
        {
            Reorder(true, 1);
        }
        protected void btnMoveDown_Click(object source, EventArgs e)
        {
            Reorder(false, 1);
        }

        protected void ReorderDef(int PTID, int defID, bool movingUp)
        {
            EPiServer.DataAbstraction.PageType pt =
                EPiServer.DataAbstraction.PageType.Load(PTID);

            foreach (EPiServer.DataAbstraction.PageDefinition d in pt.Definitions)
            {
                if (d.ID == defID)
                {
                    if (movingUp) d.MoveUp(); else d.MoveDown();
                    d.Save();

                    break;
                }
            }
            pt.Save();
        }

        protected void Reorder(bool movingUp, int moveBy)
        {
            if (lbSourceProperties.SelectedIndex == -1) return;

            int PTID = int.Parse(lbSourcePageType.SelectedValue);
            int[] indices = lbSourceProperties.GetSelectedIndices();
            if (!movingUp) Array.Reverse(indices);

            System.Collections.Generic.List<int> defIDs =
                new System.Collections.Generic.List<int>();
            foreach (int index in indices)
            {
                defIDs.Add(int.Parse(lbSourceProperties.Items[index].Value));
            }

            foreach (int defId in defIDs)
            {
                for (int j = 1; j <= moveBy; ++j)
                    ReorderDef(PTID, defId, movingUp);
            }
            
            lbPageType_SelectedIndexChanged(lbSourcePageType, new EventArgs());

            foreach (int defId in defIDs)
            {
                foreach (ListItem li in lbSourceProperties.Items)
                {
                    if (li.Value.Equals(defId.ToString()))
                    {
                        li.Selected = true;
                        break;
                    }
                }
            }
        }
    }
}

Versions

Metadata


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Thursday, January 24, 2008 11:11 AM | in EPiServer Software Development

Comments

No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 3 and 1 and type the answer here: