090978.org - A weblog about developing umbraco - an open source .NET based Content Management Platform, written by Niels Hartvig. Click here to get back to the frontpage of the weblog

12.11.2004
Creating a wiki module for umbraco using around 0.3 KLOCs of code... Permanent link to this post with (0 comments )
For a demo some time ago I made a wiki module for umbraco and for that purpose I wrote 1 (as in one) single .NET usercontrol which was 292 lines long (including comments and auto-generated code made by VS.NET). It's written in c# and is not a beauty (after all - it was just made for a demo ;-)) - links to full source follows...

Only 300 lines - am I kidding?
The reason that it's only 300 lines of code, is because umbraco allready handles content storage, versioning and user control, so all I needed to do was creating the wiki-functionality. Basically that meant:

  1. Checking for pascal cased wiki words (as in WikiWord) and then either show them as links to a page existing (and formatting them nicely with a space as "Wiki Word") or making them into a link so a new page could be made.
  2. Creating tiny rutines for either creating or updating on post back.
  3. Oh - no step three as Mr. Jobs would have said ;-)

Looking into the source...
Let's see how a new wiki page is made including a security check to see whether or not our visitor has permissions to edit the page (warning: Source ahead):

// Validate user
umbraco.BasePages.BasePage bp = new umbraco.BasePages.BasePage();
bp.ensureContext();

// Create new umbraco document
umbraco.cms.businesslogic.web.Document d = umbraco.cms.businesslogic.web.Document.MakeNew(
Request["umbWikiCreate"],
new umbraco.cms.businesslogic.web.DocumentType(_wikiEntryNodeType),
bp.getUser(),
FindWikiRootDocument(pageID).Id);

// Publish changes
d.Publish(bp.getUser());
umbraco.library.PublishSingleNode(d.Id);

That's it. And using Web Matrix or Visual Studio .NET you even get IntelliSense to make coding even easier:


IntelliSense inside Visual Studio .NET gives you any information on umbraco business objects as you write your code!

And the code for updating is almost the same:

// Validate current user
umbraco.BasePages.BasePage bp = new umbraco.BasePages.BasePage();
bp.ensureContext();

// Get the current document from the business logic
umbraco.cms.businesslogic.web.Document d = new umbraco.cms.businesslogic.web.Document(_ph.pageID);

// Assign the document property to the current value from the textbox (the document property is based
// on the alias assigned through the variable "_wikiContentAlias".
d.getProperty(_wikiContentAlias).Value = TextBoxContent.Text;

// Publish the changes
d.Publish(bp.getUser());
umbraco.library.PublishSingleNode(d.Id);

// Redirect to the updated page
Response.Redirect(umbraco.library.NiceUrl(d.Id));
Response.End();

When I had finished writing my wiki, I made a couple of Public properties and some css classes for links to make it easier to customize. Now the css part makes it easy to format the different kinds of links (whether its new pages to be created or just links to existing pages), but the public properties can be read by umbraco and makes it possible for any user to modify settings in the wiki without a single line of code - just be dialogs automatically created be umbraco. All I needed to do was:

  1. Copy the usercontrol and it's dll to my umbraco website.
  2. Creating an umbraco macro which is showed below and click the auto-discover Public properties button
  3. ...you knew it - heh, still no step three ;-)

Register the wiki in umbraco
To register my user control in umbraco it's necessary to create a macro. But it's a simple task, and just a couple of mouse clicks - look here:
Using my webbrowser I'm now inside the developer area of umbraco and by right clicking the Macro icon, I can create a new macro. So I'll be doing that and name it "Wiki" and press ok:



After that I'll edit my macro and tell it the location of my wiki control and when that is done, I get a button saying "Browse properties"... Let's try that:



Now there is the list of properties made in the usercontrol, and be checking the boxes, umbraco will create the properties inside our macro and make ui when the wiki is inserted. So let's try to insert the wiki macro into a template:




Voila - here's the result:

And the cool thing is that you can have as many wikis you want inside one single umbraco website and you can re-use other modules to work together with your wiki like the Lucene search feature I made some weeks ago, and will release at the umbraco developer lounge early next year. You can also use the template technology in umbraco to create alternative versions for RSS feeds or printing...

So a wiki is created using 0.3 KLOCs of code and registered inside umbraco with a few mouse clicks - are you getting ready for umbraco?

Imagine how fast you could create forums, FAQ-lists and other content based web modules using umbraco... The release candidate of umbraco is really close, so hold your breath and prepare to switch to a content management framework that takes both developers and editors seriously..! Please mail me if you have any questions...

You can see the full source code for the wiki here - published under GPL: wiki.ascx.cs and wiki.ascx.

0 Comment(s)...


Comments: Post a Comment


Older stuff here...

empty gif for design I love umbraco... Click here to go to the developer site

More banners here...


To the weblog frontpage
Email / Contact info
umbraco - my company
Call me through Skype
Blog archive
RSS Feed RSS Feed





commonme.org
publicMIND
urgent.dk
asynkron
Hr. Röser
Interbreed
OpenLife
Loudthinking
BlogBot.dk



Tim O'Reilly - and his books
steve mcconnel
joel spolsky
steve jobs
bruce eckel


Blogger for peace - support the opposition against the USA war for oil

This work is licensed under a Creative Commons License

empty gif for design
empty gif for design empty gif for design empty gif for design