My client wants to include random images and quotes on a web page. I need to pick randomly from a collection of existing images and quotes. (It doesn't have to be exactly random, but something similar - to be different with a new page load).
Does Umbraco do this already, or are there sample code or packages I can use?
I think .NET has an ad rotator control. Would it be easiest to just put this in a user control?
If you're doing this from a usercontrol - you can create an extension method to the umbraco.presentation.nodeFactory.Node class, that selects a random child node like this:
using System;
using umbraco.presentation.nodeFactory;
namespace My.Umbraco.Extensions
{
public static class UmbracoExt
{
/// <summary>
/// Returns a random childnode from provided node
/// </summary>
/// <param name="node">node to look in</param>
/// <returns>Node</returns>
public static Node GetRandomChild(this Node node)
{
if (node.Children.Count == 0)
return null;
if (node.Children.Count == 1)
return node.Children[0];
var rnd = new Random(DateTime.Now.Millisecond);
return node.Children[rnd.Next(0, node.Children.Count)];
}
}
}
This enables getting a random child node and its data like this in the codebehind:
using System;
using System.Web;
using System.Web.UI;
using My.Umbraco.Extensions;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.presentation.nodeFactory;
namespace petersen_gruppen.dk.usercontrols
{
public partial class TopPic : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var currentNode = Node.GetCurrent();
var picNode = currentNode.GetRandomChild()
yourImg.Src = picNode.GetProperty("umbracoFile").Value;
quoteLiteral.Text = picNode.GetProperty("quoteText").Value;
}
}
}
This code assumes you that the docs with quotes/images is childnodes to the current page, if not you can select the currentNode from its id. It also assumes you have a series of nodes of a doctype that has an upload field named "umbracoFile" for the image and a text property named "quoteText" for the text.
Here's an example that uses ImageGen to make a thumbnail of a random image. The hard-coded variables could certainly be passed in as macro variables instead.
In my example, I was making a portfolio. I pre-loaded the full-size portfolio images into the Media section. I then added a Media Picker property to my portfolio docType for the associated image. I put all my portfolio pages below a single content page (the $folder varaiable in my code). Then the macro could simply select a random node from below the $folder page and display the thumbnail using ImageGen to resize it to any size I wanted.
Be careful using the XSLT provided by Douglas. The script can cause an infinite loop, since the template GetRandomNode keeps repeating itself when it bumps into a:
The biggest problem is when you try to save the XSLT, because saving the XSLT can mysterically cause the w3wp.exe process to absorb 100% of the CPU (on /umbraco/webservices/codeeditor.save.asmx) until you recycle the application pool or restart IIS althogether. I've tested the code on multiple servers, all with the same results. Does anyone have any idea why that happens?
I rewrote the XSLT to a very basic version that fetches a random nodeID from all pages of document type RunwayTextpage:
Random quotes, images
My client wants to include random images and quotes on a web page. I need to pick randomly from a collection of existing images and quotes. (It doesn't have to be exactly random, but something similar - to be different with a new page load).
Does Umbraco do this already, or are there sample code or packages I can use?
I think .NET has an ad rotator control. Would it be easiest to just put this in a user control?
If you're doing this from a usercontrol - you can create an extension method to the umbraco.presentation.nodeFactory.Node class, that selects a random child node like this:
This enables getting a random child node and its data like this in the codebehind:
This code assumes you that the docs with quotes/images is childnodes to the current page, if not you can select the currentNode from its id. It also assumes you have a series of nodes of a doctype that has an upload field named "umbracoFile" for the image and a text property named "quoteText" for the text.
Hope this will give you an idea.
Regards
Jesper Hauge
This can also be done in XSLT.
Here's an example that uses ImageGen to make a thumbnail of a random image. The hard-coded variables could certainly be passed in as macro variables instead.
In my example, I was making a portfolio. I pre-loaded the full-size portfolio images into the Media section. I then added a Media Picker property to my portfolio docType for the associated image. I put all my portfolio pages below a single content page (the $folder varaiable in my code). Then the macro could simply select a random node from below the $folder page and display the thumbnail using ImageGen to resize it to any size I wanted.
cheers,
doug.
Thanks for the replies. Very helpful
Be careful using the XSLT provided by Douglas. The script can cause an infinite loop, since the template GetRandomNode keeps repeating itself when it bumps into a:
The biggest problem is when you try to save the XSLT, because saving the XSLT can mysterically cause the w3wp.exe process to absorb 100% of the CPU (on /umbraco/webservices/codeeditor.save.asmx) until you recycle the application pool or restart IIS althogether. I've tested the code on multiple servers, all with the same results. Does anyone have any idea why that happens?
I rewrote the XSLT to a very basic version that fetches a random nodeID from all pages of document type RunwayTextpage:
Hi,
Many thanks for supplying the code guys, very useful.
Arjan, are you saying your code example has this issue as well or have you written in a way that avoids this issue?
Doug, have you ever seen this issue with Umbraco using your code?
Many thanks
Rich
Rich, my version of the script doesn't cause an infinite loop.
is working on a reply...