Handle children with Action window plugin

Every now and then I bump in to a “problem” where I have to delete children from a node or copy/move children from one node to another. The first time I ran into this problem, I made a gui plugin for the action window to solve this for me. And now, a few years later, I thought that it might be nice to share it. 🙂

The plugin can also be useful when you have a large recycle bin that can’t be emptied. The you can delete a few pages at a time.

The plugin is really simple. You choose source node, destination node (if needed) and if you are deleting pages, you can choose how many pages you want to keep. If you choose “Count” you will also see how many descendants the page has.

Btw, when deleting, we’re starting with the page at the bottom of the page list.

Download from EPiServer World

How to get a page property value from the start page in EPiServer

When browsing through google analytics, it’s interesting to see what people search for to find your blog. So, I will try to make a few blog posts about the most frequent search terms so it will be easier to find. 🙂

If you have a property that you need to use all over the site, a global setting, then one way is to add that property to the startpage. (If you have a lot of settings it might be handier to have a special page and page type where the settings are stored.)

To access that property you first need to get the pagedata object for the start page, and then it’s easy to access the property.

var startPage = DataFactory.Instance.GetPage(PageReference.StartPage);

var property = startPage["PropertyName"];

If you are using PageTypeBuilder, you can access the property strongly typed by

var startPage = DataFactory.Instance.GetPage(PageReference.StartPage) as StartPageType;

var property = startPage.PropertyName;

Error in UI-path when upgrading from EPiServer 5 to 6

The other day we were upgrading a site for a customer from EPiServer 5 to EPiServer 6. In the upgrade process an exception was thrown telling us:

At C:\Program Files (x86)\EPiServer\CMS\6.0.530.0\Upgrade\System Scripts\Upgrade Site (SqlServer).ps1:361 char:29

+                  $newUiUrl = $uiUrl.Replace <<<< ($uiPath, $uiPath+”/CMS”)

System.Management.Automation.ParentContainsErrorRecordException: Cannot convert argument “1”, with value: “/CMS”, for “Replace” to type “System.Char”: “Cannot convert value “/CMS” to type “System.Char”. Error: “Strängen måste vara exakt ett tecken lång.””

After some research we realized that the uiUrl in episerver.config was set to https://www.somedomain.com/ui/ and the upgrade script didn’t like it and most likely expected the value to be ~/ui/.

The solution was to change it to ~/ui/ during upgrade and then change it back.

Match EPiServer properties and Page Type Builder properties (Updated)

Updated: 2011-09-21, Lee Crowe has updated this plugin. Please check out his blog post: Introducing Page Type Builder UI

Updated: This can now be found on GitHub: https://github.com/eriknordin/PTB-Helpers

Updated: 2010-11-10 16:23, see more at the end of this post!

Using PageTypeBuilder when creating EPiServer web sites?

Then you’re probably aware of when removing properties in code, they won’t change in EPiServer. Same thing when changing a property type in PTB, it won’t change in EPiServer.

In larger projects with a lot of inheritance between page types this can be really annoying, going through all page types and remove/change properties. So I created a plugin that displays miss matches between EPiServer and PTB-properties. In the plugin you can also delete unwanted properties, but for now you can’t convert them to another property type.

The plugin will appear in admin-mode:

And looks something like this.

Don’t forget to put the plugin in a secured folder (or remove it in production environment).

This plugin is tested with EPiServer CMS 6 and Page Type Builder 1.3.

Download the plugin at: http://world.episerver.com/Code/Erik-Nordin/Match-EPiServer-and-Pgae-Type-Builder-properties/

Update

Fixed a bug reported at the code section and also added a link to the page type, and also added a link to edit the property if there was a mismatch.

Make your PropertyNumber a bit wider

The default style for PropertyNumber in EPiServer is often a bit short if you have number larger than 999 and it also aligns the text to left. An easy way around this is to use EPiServer PropertyControlClassFactory.

Just create a “PropertyNumberWideControl” that inherits PropertyNumberControl and override CreateEditControls to modify the style as you feel like.

namespace Some.Web.SpecializedProperties.Controls
{
public class PropertyNumberWideControl : PropertyNumberControl
{
public override void CreateEditControls()
{
base.CreateEditControls();

base.EditControl.Style.Add("width", "100px");
base.EditControl.Style.Add("text-align", "right");
}
}
}

There are now two ways to make this control override the default PropertyNumberControl.

1.
In Web.Config, find <episerver.baseLibrary><classFactories>. In that node you might find this <add type=”EPiServer.Core.PropertyControlClassFactory, EPiServer” id=”PropertyControlFactory”>, if not add it. Then you have to register your new control and that it should override the old one: <register type=”EPiServer.Core.PropertyNumber, EPiServer” mappedType=”Some.Web.SpecializedProperties.Controls.PropertyNumberWideControl, Some.Web” />.

<episerver.baseLibrary>

<classFactories>
.....
<add type="EPiServer.Core.PropertyControlClassFactory, EPiServer" id="PropertyControlFactory">
<register type="EPiServer.Core.PropertyNumber, EPiServer" mappedType="Some.Web.SpecializedProperties.Controls.PropertyNumberWideControl, Some.Web" />
</add>
</classFactories>
</episerver.baseLibrary>

2. You can also override the control with code. To do that, take a look at this post from Ted Nyberg.

Send mail in development environment

Just realized that in web.config you can specify a directory to “send” mail to instead of using a smtp-server, pretty neat. Just change to the following where you specify the smtp settings and you won’t have any troubles with smtp-servers. Just don’t forget to change this when you really need to send a mail.

<system.net>  
   <mailSettings>  
      <smtp deliveryMethod="SpecifiedPickupDirectory">  
         <specifiedPickupDirectory 
             pickupDirectoryLocation="c:\directory\to\emails\" />  
      </smtp>  
   </mailSettings>  
</system.net>

Quick way to create checkboxlist / dropdownlist properties

In EPiServer there is a easy way to create a checkbox list property by extending some existing classes.

In this example I will fetch the children for the start page and display them. In a future blog post I will show you how to do the same with a dropdown list control, and also show you have to extend this with Settings for properties to make the properties much more dynamic and useful.

Check box list / multiple choice property

1. Create a new class and make that class extend EPiServer.Core.PropertyMultiValue. This class should also have the attribute PageDefinitionPlugIn to be recognized as a property.

[PageDefinitionTypePlugIn(DisplayName="Custom CheckBox list")]
public class PropertyCustomCheckBoxList : PropertyMultipleValue
{
public override EPiServer.Core.IPropertyControl CreatePropertyControl()
{
return new PropertyCustomCheckBoxListControl();
}
}

2. Create the property control and extend EPiServer.Web.PropertyControls.PropertySelectMultipleControlBase

public class PropertyCustomCheckBoxListControl : PropertySelectMultipleControlBase
{
}

3. Now we need to populate the property with some data. In the control class, override the method SetupEditControls and do some magic

protected override void SetupEditControls()
{
// Get children to start page
foreach (PageData page in DataFactory.Instance.GetChildren(PageReference.StartPage))
{
// Create list item
ListItem li = new ListItem(page.PageName, page.PageLink.ID.ToString());
// check if list item is selected
li.Selected = ((PropertyMultipleValue)PropertyData).IsValueActive(li.Value);
// add item to checkbox list
this.EditControl.Items.Add(li);
}
}

4. That’s it. Everything else is handled by the classes you extend. By the way, the value of the property is saved as a comma separated string.

Creating classes for PageTypeBuilder in an existing project

Update: This can now be found on GitHub: https://github.com/eriknordin/PTB-Helpers

While everyone else is creating cool gadgets for the new site center I was appointed to update an “old” site with some new functionality. And since PageTypeBuilder was introduced in my EPiServer life, and don’t feel like working without it in any project. If this would’ve been a small project I would probably just created the PTB classes by hand, but after looking into the page type list I was freaked out. Therefore I spent some time creating a “plugin” (it’s not a  real plugin, just “some” code) that creates PageTypes classes from all the page types and properties in the current project.

Step 1
The first step is to download the source code, it’s a zip-file containing a .aspx/.designer/.cs
Download it here

Step 2
Include the files in your project.

Step 3
Open you web browser and surf to the page.

Step 4
There are a few values that yo can change before doing the import/export (it’s up to you to define what it is :)).
You can add a prefix before and after the class name, change the base class that the classes should extend, enter a namespace (required) and finally decide where to put the files.
ptb-classes1

You can also exclude page types that you don’t want to include.
ptb-classes2

And finally you can decide which namespaces that should be included by default.
ptb-classes3

Step 5
When clicking “Create classes” you will see a log of classes created plus if something is excluded.
An example is that every propertyname with a “-” in it will be excluded because you can’t have “-” in a property name in C#.
When the files are created you just have to include them in your project and you should be good to go. You have to remember to include a reference to PageTypeBuilder, and you also need to change so all PageTypes .aspx.cs files inherits from the right class.

One other thing that should be mentioned is that PageTypes names like “[Something] Page Type Name” will end up like [yout catalog]\Something\PageTypeName.cs (if you don’t have any prefix).

Update – 091103 10:23
1. Discovered a bug and updated the source files.
2. Before using this, backup you database, this code is used without any warranty.
3. If you want to extend the functionality, please feel free to do it.

Update – 100128
DefaultValueType was not set which lead to some problems. Fixed.

PageTypeBuilder – makes developing with EPiServer funny

As you may have noticed over the last month, Joel Abrahamsson has created a module to EPiServer called PageTypeBuilder. In a short explanation PageTypeBuilder brings joy into developing with EPiServer! No more need for ranting around in admin mode, no more need of synchronizing page types between dev – test- stage – live! You get stronged typed access to all properties defined, you can inherit page types, you can also very easy create your own package with page types and reuse it in another project. Anyway, Joel explains it all on his own blog in four different blog posts.

So why am I writing this post when Joel explains it all?

First of all, I want to spread the PageTypeBuilder – I can guarantee that you will enjoy working with EPiServer much much more with this module.

Just a small thing like this code makes me happy.

public T1 GetDefaultPageData<T1>(PageReference parent) where T1 : TypedPageData
{
int? id = PageTypeResolver.Instance.GetPageTypeID(typeof(T1));
if (!id.HasValue)
{
throw new Exception("Could not find PageTypeID for class " + typeof(T1).ToString());
}
return (T1)DataFactory.Instance.GetDefaultPageData(parent, id.Value);
}

With this code you can create a new page of any page type you have defined with the PageTypeBuilder without having to store the id or the name of the page type in some settings page/web.config. You just need to use this nice line:

MyPageType myPage = GetDefaultPageData<MyPageType>(pageRefParent);

Code snippets

Another reason for writing this post is to say that I’ve updated the code snippets I’ve blogged about earlier.

I’ve added two snippets:

  • pt – which is the attribute for the class
  • ptppage – page reference property

I’ve also updated all the old snippets to include EditCaption and HelpText, so it’s a bit more user friendly now.

You can download the new zip-package here

Unzip the files into “My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets”.

Update: Code Snippets to PageTypeBuilder

UPDATE: The code snippets are updated, read more in this blog post.

Since the way of including properties in PageTypeBuilder has been changed, so has the snippets.

This ZIP-package includes nine different snippets:

  • ptpbool – Selected/Not selected
  • ptpdate – Date
  • ptpimage – URL to image
  • ptpnumber- Integer
  • ptpobject – You decide.. 🙂
  • ptpstring – Short string
  • ptpurl – URL
  • ptpxhtmlstring – XHTML with properties
  • ptplongstring – XHTML string with no properties

Click here to download the snippets, unzip the files into “My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets”

By the way, I found a better tool for making snippets, even though it’s called VB Snippet Editor, it handles C# snippets aswell 🙂 You can find it here: http://billmccarthy.com/Projects/Snippet_Editor/