Thursday, 18 February 2010

Tutorial: Netcommunity design mode, and Javascript

Not sure if anyone else has encountered this, but occasionally when I am editing a page and include a custom part, I'm unable to rearrange other parts on that page or add new ones.

Normally, you can move parts up or down, insert existing parts, or create new parts when you're editing a Netcommunity page. However, quite frequently I'm unable to do this after including a custom part.

I'm not quite sure why, I have a suspicion it's because a lot of my custom parts are forms that have validators in them (to check form fields have been filled in), and I think the validators might somehow be interfering with how Netcommunity's design mode works (lots of AJAX jiggery pokery, from the look of it). That's just a guess though.

Thankfully there's a simple way to get around it - instruct your custom part to check if it's being viewed in Design Mode, and if it is, don't bother displaying your part properly. Pop this into your page_load event in the Display control of your custom part:

if (this.Content.InDesignMode)

If that returns true, you can display a simple message or a dummy form, as it's not necessary to do all the legwork as the user is viewing the page from the Netcommunity admin. Remember you don't need to explicitly check if a bool returns as true - merely including a method that returns a bool in the conditional part of an if statement (the brackets) is enough. No need for "if (this.Content.InDesignMode == true)".

Bear in mind though, that the above isn't always the case - methods that return an int, for example, will cause Visual Studio to squeak at you if you try to evaluate them as a bool.

That's why Javascript is so lovely - because it does its best to evaluate everything as a bool:

javascript:if(document){ alert('true'); }else{ alert('false'); }

If the document object is present, the first block is executed. If it isn't the second block is executed. That's why Javascript is so well suited to checking if certain features are enabled in any particular browser - rather than the old practice of browser sniffing.

Because Javascript is weakly typed, you can do the same with an int:

javascript:if(1){alert('true');}else{alert('this will not be alerted');}
javascript:if(0){alert('this will not be alerted');}else{alert('false');}

I like Javascript.

Does anyone else like Javascript?

Thursday, 11 February 2010

Tutorial: Custom RSS reader for Netcommunity

Hello, it's been a while - sorry about that. Will try and post more regularly.

One thing I wanted to do recently was display some RSS on our intranet (powered by Netcommunity) - however, as you might know, the built-in RSS reader is a bit rubbish and quite inflexible in terms of displaying content. All I wanted was very simple - fetch the weather from the BBC, and display the forecast for the next 3 days in brief text format. Netcommunity's RSS reader is way too overcomplicated to accomplish something like that, so I built a very simple custom part to do the job instead.

I'll post up the full source code later on, but for now here's some snippets.

Firstly, I created a new Xml document object:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

Load in an RSS feed from wherever you choose:

doc.Load(http://www.myrss.com/rss.xml);

This bit is quite specific to what I wanted to do - fetch all of the 'item' tags:

XmlNodeList getitems = doc.GetElementsByTagName("item");

Loop over all the item tags and display them:

for (typical loop stuff) {
displaylabel.Text = displaylabel.Text + getitems.Item(i).FirstChild.InnerText;
}


Obviously depending on the XML you're dealing with, you'd need to parse it in different ways, but for this particular example (BBC weather 3-day forecast), the above is enough to print out 3 days of weather. You can then go on to customise how the text looks, by splitting the XML strings into chunks and dealing with them that way.

You can do this in any Netcommunity custom part - so for example, if you have a forum, you could create a custom part that grabs an RSS from the forum (latest posts, for example), and displays it on your homepage. Nifty.

Will post full sourcecode soon, but it will be very specific to the BBC Weather 3-day forecast - using the above, why not try it yourself? Let me know if you manage to get something working, or if you know of a better / more efficient way of fetching an RSS feed.