Archive for tag: Code Samples

Removing the "Create" option from the Context Menu in Umbraco.

Many times I've found myself becoming irritated that the "Create" option in the Umbraco UI context menu, is there even when there aren't any allowed child nodes under that node. When I click it, all I get is an empty dialog. Here's how you get rid of it.

Umbraco EventHandler: OnContentTreeBeforeNodeRender

Please, do note, this solution isn't for everyone. There might be a special case when you'd want that empty dialog. I don't know when or why, but it might happen. Also, you might ask why I keep putting code samples in images. It's because I want you to actually read what you are putting into your own source, and not just copy and paste. Copy and paste isn't a very good practice you know.

Using an UrlRewriter in Umbraco to change the paging style.

Paging using XSLT is a very useful, and fairly simple (and well-documented here). It generates very SEO friendly links between pages, however, it can be used to generate even better links, with a little tweaking and use of the UrlRewriting.NET that comes with Umbraco. The XSLT examples are from the BlogListPosts.xslt in the Blog4Umbraco package.

Note: I'm using domain prefixes, and if you don't you might have to alter the syntax in some small way to get it working flawlessly for you.

1. Add an entry to the /config/UrlRewriting.config
Code Sample - UrlRewriting, Paging

2. Modify the XSLT so that it outputs a page reference, instead of a querystring.
Was:
Code Sample - UrlRewriting, Paging - Link w/ QueryString Change into:
Code Sample - UrlRewriting, Paging - Link w/ Page Reference

Note: If you're using directory URLs, you can skip the ".aspx" ending of the link, and just add a slash.

Making objects behave across a client-server relationship. Part 2 - Setting the parent reference.

How to make sure that any objects added to the Children property, gets a correct reference to the parent object (this)?

Attempt 1: Adding Add and Insert methods to the Element

public abstract class Element {
public Element() {
this.Children = new List<Element>();
}
public int ID { get; protected set; }
public string Name { get; protected set; }
public virtual Element Parent { get; internal set; }
public virtual List<Element> Children { get; private set; }
public string Instructions { get; set; }

public void Add(Element item){
item.Parent = this;
this.Children.Add(item);
}
...
}

This solution works all-right for many cases, however, it doesn't solve the issue when the Add method is used on the List<Element> directly. We'll want this.

Attempt 2: Changing the List type

public abstract class Element {
public Element() {
this.Children = new ElementList();
}
public int ID { get; protected set; }
public string Name { get; protected set; }
public virtual Element Parent { get; internal set; }
public virtual ElementList Children { get; private set; }
public string Instructions { get; set; }
}

public class ElementList : List<Element> {
public Element Element { get; internal set; }
public ElementList(Element element) {
if (element != null){ this.Element = element; }
else { throw new ArgumentNullException("element"); }
}
new public void Add(Element item) {
if ( item.Parent == null) { item.Parent = Element; }
base.Add(item);
}
new public void Insert(int index, Element item) {
if (item.Parent == null) { item.Parent = Element; }
base.Insert(index, item);
}
new public void InsertRange(int index, IEnumerable<Element> collection){
foreach (Element e in collection){
if (e.Parent == null) { e.Parent = Element; }
}
base.InsertRange(index, collection);
}
new public void AddRange(IEnumerable<Element> collection) {
foreach (Element e in collection) {
if (e.Parent == null) { e.Parent = Element; }
}
base.AddRange(collection);
}
}

This solution ensures that all Elements added to an ElementList, gets their Parent property re-set to reference the ElementLists Element property. This way, one can always assume that an Element's Children, always have their Parent references set.

Next up: Serialization.

Making objects behave across a client-server relationship. Part 1 - The scenario.

Ok, so here's the scenario. I have a set of objects, which all inherit from Element (shown below). I need these objects to behave properly when transferred across a web service, and more importantly, when they return. One assumption I want to be able to make, is that any Element contained in the Children property, should have its Parent property set to the containing object, so it's a node-like structure. Furthermore, I need my non-default constructor logic to apply, to de-serialized objects, as if they were initialized with my parameterized constructor.

public abstract class Element {
public Element() {
this.Children = new List<Element>();
}
public int ID { get; protected set; }
public string Name { get; protected set; }
public virtual Element Parent { get; internal set; }
public virtual List<Element> Children { get; private set; }
public string Instructions { get; set; }
}

The problems:

How to make sure that any objects added to the Children property, gets a correct reference to the parent object (this)?

How to make sure that my object can be serialized in an orderly fashion?

How to enforce that initialization is done properly once the object has been de-serialized?