SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)

Occasionally you want to display a single SharePoint list/document library item at random on screen refresh. Possible uses include rotators for quotes, images, or featured documents. I did some searching on the web for a solution, but outside of writing custom web parts, I did not find anything.

I developed the following solution using SharePoint Designer and a Data View web part.

This will work well as long as you can live with the following constraints:

  • You don"t have any gaps in your ID numbers. A gap will result in a blank result on that particular refresh.
  • You have no more than 60 items

This is how:

  1. Create a new Data View web part in SharePoint Designer that displays all records in the format you want. We will end up only showing one record, so keep that in mind when you are laying this out.
  2. Open the Filter dialog
  3. Do not add any clauses, instead check the Add XSLT Filtering box and click Edit
  4. Create your XSLT filter. You can just paste in the following code:
    [@ID = (substring(ddwrt:FormatDateTime(ddwrt:TodayIso(),1033,":mm:ss"),6,2) mod count(/dsQueryResponse/Rows/Row))+1]
  5. Click Ok and save the page in SharePoint Designer
  6. Check your work in your browser. Refresh the page to see the item rotate.

Technical Details

First you need to know that the filter logic is executed for each record. You may notice that there is a Random function available in the XSLT filter. The problem is it generates a different random number for each record in the result set. I initially tried using the logic of SelectedID = RandomNumber, but I found that you cannot guarantee how many records will actually get displayed. You may end up with 0, 1, or multiple depending on the random numbers generated.

I needed logic that would get evaluated for each record that would guarantee that I would end up with exactly one result. I ended up picking the item to display based on the seconds value of the current time when the page is loaded. So if the time is currently 8:02:05, then we display the 5th item. I use the MOD function to so that we always get a hit i.e. ID = Seconds MOD NumberOfItems + 1. The main downside of this logic is that you may end up with no result if you have gaps in your IDs (i.e. you have deleted records from this particular list). Some may object that using seconds is not truly random, but in practice it is good enough for most uses.

I found that working with the FormatDateTime() function was a little frustrating. It will only let you grab certain chunks of the date and time. For instance, I could not grab just the seconds, I had to grab both the minutes and the seconds and then use the substring() function to isolate the seconds.

Enjoy!

<Clayton Leonard />

P.S. It should be possible to develop a better solution by editing the For Loop in the XSLT generated by the Data View and using the Random function to pick the record to display. This would work even if items had been deleted. The main drawback to this approach is it prevents further edits to the web part using native Data View functionality - all future changes would require editing the XSLT.  


Posted 04-26-2007 7:10 PM by wkkf

Comments

darryl2912 wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 07-27-2007 2:12 AM

Hi All,

Could you please help me with the below issue I have:

I have a field DueDate (DateTime) field type in one sharepoint 2007 list and I want to extract the value out of it and place it into another field TransmittalDueDate in document library on the same site. Once I have extracted the value from the source list  then I can copy it to the other field in the document library. Can you please urgently help me.

This is my code. However, I run into this exception can you please help me?

SPSite siteCollection = new SPSite("http://auperadev01:81");

           SPWeb destSite = siteCollection.AllWebs["SantaRitaDev01"];

           SPWeb srcSite = siteCollection.AllWebs["SantaRitaDev01"];

           SPList srcList = srcSite.Lists["Transmittals"];

           SPView srcView = srcList.Views["All Items"];

           SPListItemCollection srcItems = srcList.GetItems(srcView);

           SPDocumentLibrary IssuedTransmittalsDocLib = (SPDocumentLibrary)destSite.Lists["Issued Transmittals"];

           SPListItemCollection destItems = IssuedTransmittalsDocLib.Items;

           foreach (SPListItem srcItem in srcItems)

           {

               SPListItem newItem = destItems.Add();

               newItem["TransmittalDueDate"] = srcItem["DueDate"];

               newItem.Update();

           }

The exception is:

{"To add an item to a document library, use SPFileCollection.Add()"}

Kind Regards

darryl2912

Liz wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 02-12-2008 5:43 PM

This article is awesome!  I was looking for a Motivational Quote rotator and this works perfectly!  Thank you for taking the time to put this together.

Kevin R wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 03-05-2008 7:47 PM

I really love this solution but I was worried because I know users will want to delete records from the picture library that is the source for my 'random' data view. You mentioned that the ID's will have to remain consecutive but to work around that I changed the filter to the following and it seems to work even with missing ID's:

[position() = (substring(ddwrt:FormatDateTime(ddwrt:TodayIso(),1033,":mm:ss"),6,2) mod count(/dsQueryResponse/Rows/Row))+1]

Michael Bailey wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 03-21-2008 6:41 AM

You can also modify the select query to only return 60 items at a time according to a filter, such as the most recent... to facilitate lists with more than 60 items.  You could setup whatever kinds of filters you like to ensure a healthy mix of items.

coomsie wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 07-02-2008 12:00 AM

well done Kevin you are a start

Happy wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 07-17-2008 11:25 PM

This is great, thanks!

Mark Eggleston wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 08-04-2008 8:37 PM

This is great, but I have many more than 60 items.  Could you simply add some sort of multiplier (x2 = 120 items, x3 = 180 items) to get past the 60 item limit?  

Girish wrote re: Randomly Displaying a Single SharePoint Item on Refresh (i.e. Item Rotator)
on 03-17-2009 6:53 AM

Thanq very much

i wasted nearly 11/2 day before see this

This is great

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.