Darren Ferguson - Weblog

Thursday, February 25, 2010

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

Filed under: Umbraco - by Darren Ferguson @ 8:03 AM

Ever wished that the UmbracoAPI 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;
        }
    }
}

Thursday, February 04, 2010

Screencast: Broken Link checker for Umbraco

Filed under: Umbraco - by Darren Ferguson @ 6:52 PM

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.

Thursday, February 04, 2010

Screencast: Auto Link for Umbraco

Filed under: Umbraco - by Darren Ferguson @ 8:02 AM

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.