public override void ItemAdding(SPItemEventProperties properties)
{
if (properties.ListTitle!="Contractor Attendance")
{
properties.Cancel = true;
}
else
{
DateTime Fdate = Convert.ToDateTime(properties.AfterProperties["From_x0020_Date"]).AddDays(1);
DateTime Tdate = Convert.ToDateTime(properties.AfterProperties["To_x0020_Date"]).AddDays(1);
TimeSpan ts = Tdate.Subtract(Fdate);
int days = Convert.ToInt32(ts.Days) + 1;
for (var day = Fdate.Date; day.Date <= Tdate.Date; day = day.AddDays(1))
{
if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
{
days--;
}
}
SPWeb myweb = properties.OpenWeb();
myweb.AllowUnsafeUpdates = true;
SPList mylist = myweb.Lists["Holidays List"];
SPListItemCollection lic = mylist.Items;
foreach (SPListItem item in mylist.Items)
{
DateTime Hdate = Convert.ToDateTime(item["Date"]);
for (var day = Fdate.Date; day.Date <= Tdate.Date; day = day.AddDays(1))
{
if (day == Hdate)
{
days--;
}
}
}
myweb.AllowUnsafeUpdates = true;
SPList mylist = myweb.Lists["Holidays List"];
SPListItemCollection lic = mylist.Items;
foreach (SPListItem item in mylist.Items)
{
DateTime Hdate = Convert.ToDateTime(item["Date"]);
for (var day = Fdate.Date; day.Date <= Tdate.Date; day = day.AddDays(1))
{
if (day == Hdate)
{
days--;
}
}
}
properties.AfterProperties["Days"] = Convert.ToInt32(days);
}
}
No comments:
Post a Comment