Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#

Thought of the Day Web Part

Rate me:
Please Sign up or sign in to vote.
3.75/5 (5 votes)
19 Jun 2010CPOL2 min read 32.9K   309   7  
Thought of the day web part for SharePoint 2010 server
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.SharePoint.Utilities;


namespace TOTD.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        //Design by Lalith Gallage 
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb oWebsite = SPContext.Current.Web;
            SPList oList = oWebsite.Lists["QOTD"];
            SPListItemCollection collItem = oList.GetItems("Thought", "AuthorImage", "AuthorName");
            Random random = new Random();
            int RndItem = random.Next(1, collItem.Count+1);

            int LastDay = 0;
            int TOTD = 0;

            int CurrentDay = DateTime.Now.DayOfYear; 
            try
            {
                
                LastDay = int.Parse(Application["LastDay"].ToString());
                TOTD = int.Parse(Application["TOTD"].ToString());
                if (LastDay != CurrentDay)
                {
                    Application["LastDay"] = CurrentDay;
                    Application["TOTD"] = RndItem;

                    SPListItem oItem = collItem[RndItem-1];
                    this.ImgAuthor.ImageUrl = SPEncode.HtmlEncode(oItem["AuthorImage"].ToString().TrimEnd('?', '.', ',', ' '));
                    this.lblTOTD.Text = oItem["Thought"].ToString();
                    this.lblAuthor.Text = SPEncode.HtmlEncode(oItem["AuthorName"].ToString());
                }
                else
                {
                    SPListItem oItem = collItem[TOTD-1];
                    this.ImgAuthor.ImageUrl = SPEncode.HtmlEncode(oItem["AuthorImage"].ToString().TrimEnd('?', '.', ',', ' '));
                    this.lblTOTD.Text = oItem["Thought"].ToString();
                    this.lblAuthor.Text = SPEncode.HtmlEncode(oItem["AuthorName"].ToString());
                }
                
            }

            catch
            {

                Application["LastDay"] = CurrentDay;
                Application["TOTD"] = RndItem;
                SPListItem oItem = collItem[RndItem - 1];
                this.ImgAuthor.ImageUrl = SPEncode.HtmlEncode(oItem["AuthorImage"].ToString().TrimEnd('?', '.', ',', ' '));
                this.lblTOTD.Text = oItem["Thought"].ToString();
                this.lblAuthor.Text = SPEncode.HtmlEncode(oItem["AuthorName"].ToString());
            }

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Sri Lanka Telecom
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions