Darren Ferguson - Blog

25 February 2010 at 08:03

Quick tip: Using extension methods with Umbraco and .net 3.5

Tags: API, Education
Author: Darren Ferguson

Ever wished that the Umbraco API had an extra method or two? The example below uses .net framework 3.5 extension methods to accomplish just that.

Extension methods allow you to add new methods to existing classes without extending them.


// Today would return 2010/02/25
string path = String.Format("{0:yyyy/MM/dd}", DateTime.Now);
Doucment parent = new Document(id);
Document newDoc = parent.MakePath(path, new DocumentType(parent.ContentType.Id), true);

// MakePath isn't an Umbraco API method, so we'll add it using extension methods.
namespace FM.Umbraco
{
    public static class DocumentExtensions
    {
        public static Document MakePath(this Document d, string path, DocumentType dt, bool publish)
        {
            Document parent = d;
            User u = new User(0);
            foreach (string component in path.Split('/'))
            {
                int parentId = parent.Id;
                Document child = parent.Child(component);

                if (child != null) { 
                    parent = child; 
                }
                else
                {
                    parent = Document.MakeNew(component, dt, u, parentId);
                }

                if (publish)
                {
                    parent.Publish(u);
                    umbraco.library.UpdateDocumentCache(parent.Id);
                }
            }
            return parent;
        }

        public static Document Child(this Document d, string name)
        {
            foreach (Document child in d.Children)
            {
                if (child.Text.Equals(name))
                {
                    return  child;
                }
            }
            return null;
        }
    }
}

04 February 2010 at 18:52

Screencast: Broken Link checker for Umbraco

Tags: JavaScript, Link checker, Social Issues, Technology Internet
Author: Darren Ferguson

This screencast is a demonstration of Broken Link checker for Umbraco. Comments, suggestions and feedback are welcome.

The Camtasia Studio video content presented here requires a more recent version of the Adobe Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

04 February 2010 at 08:02

Screencast: Auto Link for Umbraco

Tags: JavaScript, Social Issues, Technology Internet
Author: Darren Ferguson

This screencast is a demonstration of Auto Link for Umbraco. Comments, suggestions and feedback are welcome.

The Camtasia Studio video content presented here requires a more recent version of the Adobe Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.