Click here to Skip to main content
13,249,504 members (35,119 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 18 Apr 2008

Quote of the Day (QOTD) Webpart

, 18 Apr 2008
Rate this:
Please Sign up or sign in to vote.
An article on how to add a quote of the day using SharePoint Server

Quote of the Day Web Part


Quote of the Day is an arbitrary message, and was traditionally a random selection from a list of notable quotes. An interesting idea is setting-up a company with a QOTD server and providing their users with some useful information. Or you can add a connection to a QOTD server to the end of their login script to start your day with a refreshing insight.

Where Quote of the Day is Useful

In my company internal portals, I have to implement the QOTD, Tip of the Day etc., so to generalize the concept I wrote a simple Webpart which asks the users to enter the website, Custom List, Field Name (Quote) from Webpart ToolPane.


  • Developer knows how to create a Webpart and how to deploy it to a MOSS/WSS portal site.
  • Developer knows how to add a Webpart to Webpart Gallery.


  • Visual Studio 2005 with Extensions for Windows Sharepoint Services 3.0 for creating Web parts


The main concept is that we have to get different quote for everyday. For example, we have to randomly get a quote from the quotes repository. For that reason I have created a Custom List in a web portal site. I can share this list with the other website by using a Webpart.

Using the Code

If you want to add the ‘Quote of the Day’ to your home or default page, we can add it by adding embedded Third Party JavaScript to the Page, or by calling the Third Party webservice(s). Or we can do the same without accessing third party webservice(s) or servers easily, using the achieve in SharePoint Web Portal.

Sample QOTD Webpart Page

The following picture shows the QOTD Webpart.

Fig2: Sample.

Code for the Random Number for the Day

Code 1:

Random r = new Random(DateTime.Now.DayOfYear);
int iOutput = r.Next(MaxValue);
//Returns a nonnegative random number less than the specified maximum value(MaxValue)

The code for the quote of the day after selection of SiteUrl, ListName, Quote, Field Name.

Code 2:

SPSite site = new SPSite(this.SiteUrl);
SPList QuoteList = (SPList)site.OpenWeb().Lists[new Guid(this.ListName)];
int iMax = QuoteList.GetItems(QuoteList.Views["All Items"]).Count;
String[] strQuote = new string[iMax];
int i = 0;
foreach (SPListItem listItem in QuoteList.Items)
    strQuote[i] = listItem[new Guid(QuoteFieldName)].ToString();
//get the Random number
int iRandomNumber = getRandomNumberByDay(iMax);
if (strQuote.Length > 1)
    Quote = strQuote[iRandomNumber];
    Quote = strQuote[0];

My logic in the above code is, first I am getting the reference site, then after that getting the list for the QOTD, then I am finding the maximum number of quotes in the list to restrict the random number to be less than the maximum number of Quotes(count). Then assign all the quotes to a string array.

Another, similar method follows:

Code 2:

SPSite site = new SPSite(this.SiteUrl);
SPList QuoteList = (SPList)site.OpenWeb().Lists[new Guid(this.ListName)];
SPView AllQuotesView = QuoteList.Views["All Quotes"];
SPListItem listItem = null;
listItem = QuoteList.GetItemById(
Quote = listItem["Quote"].ToString();

We can assign the Quote from the above code, but not if the Contributor(user) deletes a ListItem in the middle. In the internal database, SharePoint deletes the associated ID. If the same id is returned by the our user-defined function getRandomNumberByDay(..) eventually it gives an error.

1If we all did the things we are capable of doing, we would literally astound ourselves. (Thomas Alva Edison )
2An average person with average talents and ambition and average education, can outstrip the most brilliant genius in our society, if that person has clear, focused goals. (Mary Kay Ash)
3Action may not always bring happiness, but there is no happiness without action. (Benjamin Disraeli)
4Well done is better than well said. (Benjamin Franklin)

From the above List, if the contributor deletes a list Item ‘3’. Then the Quote of the Day WebPart code 2 will not work since, the random function code may return id 3 or 0, the List Item with id 0 or 3 does not exist.

The Steps for ‘How to Add the QOTD Webpart’ to the Page

  1. Create a Webpart Page (here I am adding to the default Page)
  2. The following screen shows hat happens if you edit a page.

    Fig2: How to Add Web Part to Basic Web Part Page.

  3. The following screen shows hat happens if you edit a page.

  4. Click on ‘Add a Web part’ (here I selected in Right Zone).

    Fig4: How to Add Web Part to Basic Web Part Page.

  5. From the Webpart Gallery, Scroll Down & Select the ‘QuoteofTheDayWP’ Web Part. Check the Adjcent Check box and Click on ‘Add’ button. It Shows the following screen. Click on ‘Open the tool pane’.

    Fig5: How to Add Web Part to Basic Web Part Page.

  6. Open the Tool Pane associated to the webpart and enter the Site URL and select other details such List (‘Quote of the Day’) and List Item for ‘Quote’and click on Ok.

    Fig6: How to Add Web Part to Basic Web Part Page.

    Fig7: How to Add Web Part to Basic Web Part Page.

    Fig8: How to Add Web Part to Basic Web Part Page.

    Fig8:Final Result Page

Points of Interest

You can use the same code with a few changes, for ‘Tip of the Day’, ‘Quiz of the Day’ etc.

Please give your comments.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Surya Prakash Adari
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralIndex was outside the bounds of the array. Pin
Martinmc21-Apr-08 19:46
memberMartinmc21-Apr-08 19:46 
GeneralRe: Index was outside the bounds of the array. Pin
Surya Prakash Adari21-Apr-08 22:10
memberSurya Prakash Adari21-Apr-08 22:10 
GeneralRe: Index was outside the bounds of the array. Pin
Martinmc23-Apr-08 14:49
memberMartinmc23-Apr-08 14:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 18 Apr 2008
Article Copyright 2008 by Surya Prakash Adari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid