Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

Assumptions

  • 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.

Software

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

Concept

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();
        i++;
    }
    //get the Random number
    int iRandomNumber = getRandomNumberByDay(iMax);
    if (strQuote.Length > 1)
        Quote = strQuote[iRandomNumber];
    else
        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(
        getRandomNumberByDay(QuoteList.GetItems(AllQuotesView).Count));
    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.

Field ID Quote
1 If we all did the things we are capable of doing, we would literally astound ourselves. (Thomas Alva Edison )
2 An 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)
3 Action may not always bring happiness, but there is no happiness without action. (Benjamin Disraeli)
4 Well 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.

License

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

Comments and Discussions

 
GeneralIndex was outside the bounds of the array. PinmemberMartinmc21-Apr-08 18:46 
GeneralRe: Index was outside the bounds of the array. PinmemberSurya Prakash Adari21-Apr-08 21:10 
GeneralRe: Index was outside the bounds of the array. PinmemberMartinmc23-Apr-08 13:49 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 18 Apr 2008
Article Copyright 2008 by Surya Prakash Adari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid