Some weeks ago i wrote an article about creating a birthday webpart. The problem with that webpart is that the list every time has to be updated to refresh the coming birthdays.
This is an example of a custom webpart which retrieves its information from a ContactList. The WebPart contains three properties:
ContactListName is the name of the contactlist containing the people and their DayOfBirths
DayOfBirthField is the name of the field containing the DayOfBirth
IntheNextDays is the number of days we have to look forward
The RenderContact() method loops through all the SPListItems of the ContactList and per item some DateTimes and TimeSpans are calculated. I can imagine that somebody could make this a little bit easier but it is just an example which actually works!
We compare the timespans and make sure that if IntheNextDays is in the next year also those birthdays are shown.
In this example we only show the "First Name" and "Last Name" fields. It is up-to-you to make it more beautiful. :)
public class BirthDayWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private string contactListName = string.Empty;
private string dayOfBirthField = string.Empty;
private int inTheNextDays = 14;
[Browsable(true),
Category("Birthday properties"),
DefaultValue(14),
WebPartStorage(Storage.Shared),
FriendlyName("In The Next Days"),
Description("In The Next Days")]
public int InTheNextDays
{
get
{
return inTheNextDays;
}
set
{
inTheNextDays = value;
if (inTheNextDays >= 365)
{
inTheNextDays = 364;
}
}
}
[Browsable(true),
Category("Birthday properties"),
DefaultValue(""),
WebPartStorage(Storage.Shared),
FriendlyName("ContactList Name"),
Description("ContactList Name")]
public string ContactListName
{
get
{
return contactListName;
}
set
{
contactListName = value;
}
}
[Browsable(true),
Category("Birthday properties"),
DefaultValue(""),
WebPartStorage(Storage.Shared),
FriendlyName("DayOfBirth Field"),
Description("DayOfBirth Field")]
public string DayOfBirthField
{
get
{
return dayOfBirthField;
}
set
{
dayOfBirthField = value;
}
}
/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
RenderContacts(output);
}
private void RenderContacts(HtmlTextWriter output)
{
try
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList contacts = currentWeb.Lists[contactListName];
foreach(SPListItem currentContact in contacts.Items)
{
DateTime dtItem = Convert.ToDateTime(currentContact[DayOfBirthField]);
DateTime dtNow = DateTime.Now;
DateTime dtThisYear =
new DateTime(dtNow.Year, dtItem.Month, dtItem.Day);
DateTime dtNextYear = new DateTime(dtNow.Year + 1, dtItem.Month, dtItem.Day);
DateTime dtEndOfThisYear = new DateTime(dtNow.Year, 12, 31);
TimeSpan tsTilEndOfThisYear = dtEndOfThisYear - dtNow;
TimeSpan tsThisYear = dtThisYear - dtNow;
TimeSpan tsNextYear = dtNextYear -
new DateTime(dtNow.Year + 1, 1, 1);
int DaysInNewYear = InTheNextDays - tsTilEndOfThisYear.Days;
if ((tsThisYear.Days < InTheNextDays && tsThisYear.Days >= 0) ||
(DaysInNewYear > 0 && tsNextYear.Days < DaysInNewYear))
{
output.WriteLine(currentContact["First Name"] + " " + currentContact["Last Name"]);
output.WriteLine("<BR>");
}
}
}
catch(Exception e)
{
output.WriteLine(e.Message);
}
}
}
The BirthDay WebPart version 1 is now downloadable. Follow the link:
TamTam_BirthDay_WebPart.msi (132 KB)