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.
Code for the Random Number for the Day
Code 1:
Random r = new Random(DateTime.Now.DayOfYear);
int iOutput = r.Next(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++;
}
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
- Create a Webpart Page (here I am adding to the default Page)
- The following screen shows hat happens if you edit a page.
- The following screen shows hat happens if you edit a page.
- Click on ‘Add a Web part’ (here I selected in Right Zone).
- 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’.
- 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.
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.