Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Quote of the Day (QOTD) Webpart

2.33/5 (3 votes)
18 Apr 2008CPOL4 min read 1   490  
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:

C#
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:

C#
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:

C#
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.

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

    Image 4

  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)