Allyis Home
Follow Allyis: Follow Allyis on TwitterFollow Allyis on LinkedInFollow Allyis on FacebookSign up for the newsletter

Randomly Displayed Content in SharePoint

Icon signifying this is a WhitepaperDownload Article
Article | Justin Botchek Bookmark and Share

On the most recent SharePoint site I’ve worked on, I was tasked with finding a way to randomly select pieces of content to display on the homepage after each page refresh. I’ve seen more complicated methods to implementing this using JavaScript, but found the functionality can be easily accomplished using only two lines of xsl code within an existing SharePoint data view web part. The lines are:

For this example I will be manipulating code that has been automatically generated by SharePoint Designer to display a simple multiple item data view. In a multiple item view, each item from the data source is collected via the Rows parameter. By default the data view will loop through all rows until every item has been displayed, but in this case we only want it to randomly select one of those items for display. Thankfully there is a built-in XSL function we can use to choose that random number for us. SharePoint also tracks each row item with a value called ‘position()’; so in order to randomly pick a row we simply need to generate a random number between one and the total number of rows and then filter the results by that position value.

Normally, a generic multiple item data view generated by SharePoint Designer will look something like this:

To randomly select one item to display, we search through the data view code for where it cycles through each row. It will be in a template block that looks like this:

Directly above the line shown in bold, we will want to add in the line to select the random position value:

This creates a variable ‘highlight’ and assigns a random value to it using ‘ddwrt:Random’ between 1 and the total number of rows ‘count($Rows))’. Finally, we will want to modify the line in bold so that it looks like this:

This applies a filter ‘*position()=$highlight+’ to the xsl for-each statement so that it will only display rows with a position equal to the randomly selected value from the previous line. After this change, save the page changes and reload the page with your data view. The data view shown above would look something like this:

You will notice that the data view now only displays one entry, and after a couple page refreshes it will cycle randomly through the other items. Real world applications of this could be to display a different random ‘fun fact’ on a site home page whenever someone visits, or even to draw random user names out of a SharePoint ‘hat’.

 

 Our Solution Areas

Allyis designs, develops and supports solutions in the following areas to help organizations operate and excel in the next generation business environment.

Collaboration
Make it easier for people to work together, innovate, and find information.

Knowledge Management
Leverage the power of business information, knowledge and expertise.

Business Process
Integrate processes, people, and information across lines-of-business or locations.

Business Insights
Enable agile and informed decisions with access to business intelligence and insights.

Engagement
Inspire adoption by ensuring users can easily access the people and information they need.
Contact Us
425.691.3000
Or, click below & we'll
get back to you.
Contact Us