at this example i'm using INI file again (read up my previous post to see it) to update Sharpeoint List for 1 year calendar
C#
IniReader oINI = new IniReader(PATH_TO_INI_FORMAT_HERE);
myPWAUrl = oINI.ReadString("DOMAIN", "PWA");
myCalendarList = oINI.ReadString("DOMAIN", "Calendar");
static void UpdateCalendar()
{
SPSite oSite = new SPSite(myPWAUrl);
SPWeb oWeb = oSite.OpenWeb();
//list is already exists then delete it
oWeb.Lists[myCalendarList].Delete();
oWeb.Update();
oWeb.Lists.Add(myCalendarList, "Enterprise Holidays", SPListTemplateType.Events);
oWeb.Update();
SPList oList = oWeb.Lists[myCalendarList];
oList.OnQuickLaunch = true;
oList.Update();
DateTime StartDate = new DateTime(System.DateTime.Today.Year, 1, 1);
DateTime CurrentDate;
for (int i = 1; i <= 365; i++) { if (i > 1)
{
CurrentDate = StartDate.AddDays(i - 1);
if (CurrentDate.DayOfWeek.ToString() == "Saturday"
CurrentDate.DayOfWeek.ToString() == "Sunday")
{
SPListItem oItem;
oItem = oList.Items.Add();
oItem["Title"] = "Holiday";
oItem["Start Time"] = CurrentDate.ToString("dd MMM yyyy 00:00");
oItem["End Time"] = CurrentDate.ToString("dd MMM yyyy 23:59");
oItem["All Day Event"] = "True";
oItem.Update();
}
//Console.WriteLine("{0} - {1}",
// StartDate.AddDays(i - 1).ToString("dd MMM yyyy 00:00"),
// StartDate.AddDays(i - 1).ToString("dd MMM yyyy 23:59"));
}
else
{
CurrentDate = StartDate;
if (CurrentDate.DayOfWeek.ToString() == "Saturday"
CurrentDate.DayOfWeek.ToString() == "Sunday")
{
SPListItem oItem;
oItem = oList.Items.Add();
oItem["Title"] = "Holiday";
oItem["Start Time"] = CurrentDate.ToString("dd MMM yyyy 00:00");
oItem["End Time"] = CurrentDate.ToString("dd MMM yyyy 23:59");
oItem["All Day Event"] = "True";
oItem.Update();
}
//Console.WriteLine("{0} - {1}",
// StartDate.ToString("dd MMM yyyy 00:00"),
// StartDate.ToString("dd MMM yyyy 23:59"));
}
if (CurrentDate.Year != System.DateTime.Today.Year) break;
}
}
the result will be like in attached
until then :D
PS: u must use Calendar type as the new list
Comments