sreenu kolusu

sreenu kolusu

  • NA
  • 38
  • 181.4k

Get Leave Days with out holidays & Week Days Using Sharepoint 2010 Event Receivers

Aug 9 2012 2:24 PM
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--;
                            }
                        }

                    }
            

               properties.AfterProperties["Days"] = Convert.ToInt32(days);
           }
          
       }