protected void btnEditDelegation_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            int delegationID = int.Parse(e.CommandArgument.ToString());

            InitializeWebAppProperties();

            if (siteURLForDelegationsList != null)
            {
                using (SPSite delegationSite = new SPSite(siteURLForDelegationsList))
                {
                    DelegationsDataContext dc = new DelegationsDataContext(delegationSite.RootWeb.Url);
                    EntityList<Delegation> delegations = dc.GetList<Delegation>("Delegations");

                    Delegation selectedDelegation = delegations.Where(d => d.Id == delegationID).FirstOrDefault();

                    PopulateDelegation(selectedDelegation);
                }
            }
        }
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            InitializeWebAppProperties(properties);

            if (siteURLForDelegationsList != null)
            {

                using (SPSite delegationSite = new SPSite(siteURLForDelegationsList))
                {
                    DelegationsDataContext dc = new DelegationsDataContext(delegationSite.RootWeb.Url);
                    EntityList<Delegation> delegations = dc.GetList<Delegation>("Delegations");

                    SPUser assignedToUser = GetSPUser(properties, "Assigned To");

                    Delegation delegation = delegations.FirstOrDefault();

                    var currentDelegations = delegations.Where(d => d.DelegationForId == assignedToUser.ID)
                        .Where(d => d.StartDate <= System.DateTime.Now)
                        .Where(d => d.EndDate >= System.DateTime.Now)
                        .ToList();

                    if (currentDelegations != null)
                    {
                        //If there are any valid delegations. A legal delegation takes precedence over others.
                        if (currentDelegations.Where(d => d.DelegationType == DelegationType.Legal).FirstOrDefault() != null)
                        {
                            AssignDelegation(properties, currentDelegations.Where(d => d.DelegationType == DelegationType.Legal).First());
                        }
                        else if (currentDelegations.Where(d => d.DelegationType == DelegationType.Manager).FirstOrDefault() != null)
                        {
                            AssignDelegation(properties, currentDelegations.Where(d => d.DelegationType == DelegationType.Manager).First());
                        }
                        else if (currentDelegations.Where(d => d.DelegationType == DelegationType.Self).FirstOrDefault() != null)
                        {
                            AssignDelegation(properties, currentDelegations.Where(d => d.DelegationType == DelegationType.Self).First());
                        }
                    }
                }
            }

            base.ItemAdding(properties);
        }
        void btnSubmit_Click(object sender, EventArgs e)
        {
            InitializeWebAppProperties();

            if (siteURLForDelegationsList != null)
            {

                using (SPSite delegationSite = new SPSite(siteURLForDelegationsList))
                {
                    DelegationsDataContext dc = new DelegationsDataContext(delegationSite.RootWeb.Url);
                    EntityList<Delegation> delegations = dc.GetList<Delegation>("Delegations");

                    string[] userValues = spPeoplePicker.CommaSeparatedAccounts.Split(',');

                    SPUser assignedToUser = SPContext.Current.Web.EnsureUser(userValues[0]);

                    Delegation delegation;

                    if (string.IsNullOrEmpty(delegationID.Value))
                    {
                        delegation = new Delegation()
                        {
                            DelegationFor = SPContext.Current.Web.CurrentUser.LoginName,
                            DelegationForId = SPContext.Current.Web.CurrentUser.ID,
                            StartDate = calDelegateFrom.SelectedDate,
                            EndDate = calDelegateTo.SelectedDate,
                            AssignedTo = assignedToUser.LoginName,
                            AssignedToId = assignedToUser.ID,
                            DelegationType = DelegationType.Self
                        };

                        delegations.InsertOnSubmit(delegation);
                    }
                    else
                    {
                        delegation = delegations.Where(d => d.Id == int.Parse(delegationID.Value)).FirstOrDefault();

                        if (delegation != null)
                        {
                            delegation.StartDate = calDelegateFrom.SelectedDate;
                            delegation.EndDate = calDelegateTo.SelectedDate;
                            delegation.AssignedTo = assignedToUser.LoginName;
                            delegation.AssignedToId = assignedToUser.ID;
                            delegation.DelegationType = DelegationType.Self;
                        }
                    }

                    dc.SubmitChanges();

                }
            }

            //Reload the delegations
            LoadDelegations();
            ResetNewDelegationPanel();
        }
        private void LoadDelegations()
        {
            if (siteURLForDelegationsList != null)
            {
                using (SPSite delegationSite = new SPSite(siteURLForDelegationsList))
                {
                    using (DelegationsDataContext dc = new DelegationsDataContext(delegationSite.RootWeb.Url))
                    {
                        EntityList<Delegation> delegations = dc.GetList<Delegation>("Delegations");

                        List<Delegation> myDelegations = delegations
                            .Where(d => d.DelegationForId == SPContext.Current.Web.CurrentUser.ID)
                            .ToList();

                        grdAllDelegations.DataSource = myDelegations;
                        grdAllDelegations.DataBind();
                    }

                }
            }
        }
        private void LoadDelegations()
        {
            if (siteURLForDelegationsList != null)
            {
                if (directReports != null)
                {
                    using (SPSite delegationSite = new SPSite(siteURLForDelegationsList))
                    {
                        SPWeb rootWeb = delegationSite.RootWeb;
                        rootWeb.AllowUnsafeUpdates = true;
                        rootWeb.Update();

                        using (DelegationsDataContext dc = new DelegationsDataContext(delegationSite.RootWeb.Url))
                        {
                            EntityList<Delegation> delegations = dc.GetList<Delegation>("Delegations");

                            List<int> drUsers = new List<int>();

                            foreach (UserProfile directReport in directReports)
                            {
                                SPUser drUser = delegationSite.RootWeb.EnsureUser(directReport.AccountName);
                                drUsers.Add(drUser.ID);
                            }

                            List<Delegation> myDelegations = delegations
                                .Where(d => drUsers.Contains(d.DelegationForId.Value))
                                .ToList();

                            grdAllDelegations.DataSource = myDelegations;
                            grdAllDelegations.DataBind();
                        }

                        rootWeb.AllowUnsafeUpdates = false;
                        rootWeb.Update();
                    }
                }
            }
        }
        void btnSubmit_Click(object sender, EventArgs e)
        {
            string siteURLForDelegationsList = Utility.GetDelegationsListSite();

            if (siteURLForDelegationsList != null)
            {

                using (SPSite delegationSite = new SPSite(siteURLForDelegationsList))
                {
                    DelegationsDataContext dc = new DelegationsDataContext(delegationSite.RootWeb.Url);
                    EntityList<Delegation> delegations = dc.GetList<Delegation>("Delegations");

                    string selectedUser = ddlSubordinates.SelectedValue;

                    SPUser assignedToUser = SPContext.Current.Web.EnsureUser(selectedUser);

                    Delegation delegation = new Delegation()
                    {
                        DelegationFor = SPContext.Current.Web.CurrentUser.LoginName,
                        DelegationForId = SPContext.Current.Web.CurrentUser.ID,
                        StartDate = calDelegateFrom.SelectedDate,
                        EndDate = calDelegateTo.SelectedDate,
                        AssignedTo = assignedToUser.LoginName,
                        AssignedToId = assignedToUser.ID,
                        DelegationType = DelegationType.Self
                    };

                    delegations.InsertOnSubmit(delegation);

                    dc.SubmitChanges();

                }
            }

            //Set value for message
        }