Skip to main content

Mark Saturday and Sunday as Holiday in Sharepoint List


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

Popular posts from this blog

SBO - Internal Error (-5002) Occured in Inventory Transfer

this is something that not well documented in SBO, it took me several hours to look at Google and can't find anything about it. Thank God friend of mine got answer for this one.So the Sympton is something like this, u need to make Inventory Transfer for Batch Item and Serial Item in one single Transaction so the Transaction is the combination of it. ex in detail lines: 1. Serial Item 2. Batch Item 3. Batch Item 4. Batch Item 5. Serial Item 6. Serial Item . When u reach this code oStockTransfer.Add it will return an error "Internal Error (-5002) Occured". This is because ur details is not in order, meaning u need to order the line with combination of Batch first then Serial or Serial first then Batch so be careful with this thing :D see u

Resolve Collation error in SQL Server

i often get this error when trying to JOIN some table Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation. u need to use COLLATION when performing the JOIN, something like this SELECT field1 FROM table1 INNER JOIN table2 ON table1.field3 COLLATE DATABASE_DEFAULT = table2.field4 COLLATE DATABASE_DEFAULT i found this when trying to JOIN, maybe it affect other areas..until i found one see u

Crystal Report 2010 - Failed to load database information

"Failed to load database information" error when i try deploy my program with CR 2010 using TTX file as report definition this is because there is missing .dll, call "CRDB_FIELDDEF.DLL"..u need to manually copy the file to "C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86" folder then the report is loading again it said that TTX is old tech but i'm still using it because i don't need to connect the report to any DB, but until i found new one to replace :D check this link to read it [EDIT 25 Jan 2011] i think i'll using ADO.NET Dataset next time :D until then