Random quote

Update [01/03/2009]: I have corrected an error in the script I initially published.

Three months ago, I published a script to display a random picture on your SharePoint site.

To answer Chris’ question, here is a variation to display a random quote. The method is the same as for the random picture, except that in this case I am using a SharePoint custom list to host the quotes. For my sample script, the quote is entered in the default Title column (SharePoint calls it “ows_LinkTitle”).

Note that the script and the list don’t need to be on the same site, they just need to be on the same server.


<DIV ID="QuotePlaceholder"></DIV>
<script type="text/javascript">
//
// Random Quote
// Questions and comments: Christophe@PathToSharePoint.com
//

//
// Enter the parameters for your list or library: ListID, ServerURL, SiteURL
//

var ListID = "{7FE93484-962F-4FC7-B450-4F79A0622219}";
var ServerURL = "http://theServer";
var SiteURL = "http://theServer/sites/theSite";

// Start quote retrieval

var URLprotocolURL = SiteURL + "/_vti_bin/owssvr.dll?Cmd=Display&XMLDATA=TRUE&List=" + ListID ;

// Create different XMLHttpRequest objects according to different browsers

var xmlhttp=null;
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
alert("Your browser does not support XMLHTTP.");
}
}
}

xmlhttp.onreadystatechange=StateChange;

// The item will be loaded asynchroneously

xmlhttp.open("GET",URLprotocolURL,true);
xmlhttp.send(null);

function StateChange()
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200)
{
DisplayQuote();
}
else
{
alert("Problem retrieving XML data");
}
}
}

function DisplayQuote()
{
var xmlDoc = xmlhttp.responseXML;
var ListItems = xmlDoc.getElementsByTagName("z:row") || xmlDoc.getElementsByTagNameNS("*","row") ;
var SelectedItem = Math.floor(Math.random()* ListItems.length);
var SelectedQuote = ListItems[SelectedItem].getAttribute("ows_LinkTitle");
document.getElementById("QuotePlaceholder").innerHTML = SelectedQuote ;
}
</script>
Advertisements

32 thoughts on “Random quote

  1. Hi, thanks for your really usefull scripts! I have your imagerotator working nice and smoth, but this one shows nothing, think the problem is the function for DisplayQuote. Any tip?

  2. To answer questions I received by e-mail: this can be applied to other list types and to fields other than the title. I’ll publish another example using an announcement list (as this is typically the kind of content you may want to share across sites).

  3. Thanks for the code but I am having a problem, it shows nothing. I have the site URL, list GUID, and the LinkTitle all filled out with information. Any ideas what the problem could be?

  4. Update: there was an error in the last line of the script and I have corrected it.
    My apologies to DrWat, Michael and those who have tried to use the code.

  5. You would think that using

    var SelectedQuote = ListItems[SelectedItem].getAttribute(“ows_Body”);

    Would display the body of an announcements list, but it returns “null” for me. Any ideas?

    • Interestingly, I had the same issue. A tip from Larry about using different names for the fields led me to the idea that my FieldInternalName and FieldName were the same and for the purposes of this script, shouldn’t be. To solve that, I renamed the field from “Body” to “body”…. and that seemed to do the trick. I now had two (similar) names for the field.

  6. I want to piggy back on Walter’s comment. I have been trying to figure out what the other ows attributes are for other fields. Is there a list or resource that identifies how or where to find these names?

  7. With a little trial and error, I found that I was able to write it by using ows_ + “FieldInternalName”. Hope this helps.

  8. View the source of the NewForm.aspx, find the field in the page and look up the FieldInternalName. I have found that with Multiline fields the FieldName works.

  9. This is what I have tested.
    if FieldType=”SPFieldNote” then use ows_+”FieldName”
    if FieldType=”SPFieldText” then use osw+”FieldInternalName”, this is not true for all. I created a second field and the name FieldInternalName is quote0, it returns null.

    I am not sure why, but still looking. also looking what is use for calculated fields
    if FieldType=”SPFieldCalculated”

  10. The script works on FireFox – new item returned with every screen refresh – however, on IE 7 the webpart goes blank on refresh. Should it work on IE?

    thanks again.

  11. Unless otherwise stated, all the scripts I propose are for both IE and Firefox.
    On refresh, IE simply re-runs the script and you should get another random value. It is working for me…

  12. Question-if you wanted to change the font for the quote, would you do that in this script, or in the list you’ve created for the quotes? The script works great, but the quote is very small (about a size 8). Thanks for publishing this! It’s awesome.

  13. Hi,
    how is it possible to make the random-displayed text change in a hyperlink to the displayed item?

    so that you can click on the link a land on the dispform.aspx…

  14. Hi,

    Is it possible (I’m thiking it is 😉 to get more columns from the custom list and concatenate them to be one text?

    Say, the list has 2 columns Name and Message and the contents are Name: “Andrey”; Message: “Hi, Christophe.” So, I need the webpart to show: “Hi, Christophe. [new line] Andrey”

    Great site by the way!

    • Absolutely. You have two ways to do this:
      – either use a calculated column that concatenates the two columns
      – or do the concatenation in the script

  15. Christophe,

    Thanks! I figured out how to concat in script. Works great!

    How about taking this thing further and having it switch a quote (i my case – message) periodically? Time to upgrade this thing!

  16. Hey,

    This site is amazing, thank you for all!

    I am not a coder neither good in scripts. What I would like to do is to display a kind of random “Did you know…?”. What I miss so far is to display an hyperlink to the random word and have this a bit fancier. I have seen that Christophe gave the tips, but I am not able to figure out concretly how to do it in the script. Could you help me?

  17. How I can return two values from list?
    getAttribute(“ows_LinkTitle”); is there any way to use + or something to return second value?

  18. Thanks for this great code, I got it working just fine.

    Just one quick question? Is it hard to change the random logic to a today’s date logic?

    I’m no Javascript expert and I don’t want to begin customising if it’s too difficult. Maybe I would have to create a date column in the list to accompany the title/quote coulmn.

    Regards
    Ian Smith

  19. Hi Christophe,

    Just to enhance on the CSS for the CEWP.
    I’m no coder, so I’m trying to add the
    style=”font-size:12px;”
    to the code but have no idea where to place it. I’m actually trying to make my list of 500 quotes in Italics and if it can be done, quotation marks at the beginning and end of each quote.
    Are you able to provide the exact code and which line I’m to put this in?

    “Research is what I’m doing when I don’t know what I’m doing. Wernher von Braun”

    • Brett: Simply replace the very first line in Christophe’s code with this:

      <DIV ID="QuotePlaceholder" style="font-style:italic;font-size:12px;"></DIV>
      

      This will make the font size 12px and font style italics. For the quotes at the beginning and end, I’m sure the code could be modified to concatenate these in there, but why not just add them into the quotes from your list?

      Hope this helps.

  20. Argghh….it seems I can’t post html here without it being stripped out. I’ll post to my twitter account under @planetparker.

    Christophe: How can I post html to your blog? Newbie here 🙂

  21. Hi Christophe,

    Are you able to modify the code to function on SP2010? Currently migrating web parts from 2007 and I’m getting a XML error.

    Many thanks.

Comments are closed.