Search code examples
c#sharepointevent-receiver

SharePoint Add Days to a Date Field Using C#


I am currently using an event receiver in a SharePoint site meant to onboard new employees. New user info is entered into a list called "New Hire Profile" and a second list called "Access Profile" is created based on the new hire profile list. There is a field on the employee profile called "Start Date" which is a simple SharePoint date content type. I am trying to create an calculated "Due Date" field in the access profile for tasks due on the start date. I would like to add the "StartDate" which is a date picker and "Dueindays" which is a numeric field listed on a third list "newHireTaskTemplate". The code is setup to pull the info from the fields and copy them to the new list. I am unsure how to go about adding days to the date picker date. My code currently looks like this:

accessProfile["Title"] = employeeProfile.ContentType.Name;
accessProfile["FirstName"] = employeeProfile["FirstName"];
accessProfile["LastName"] = employeeProfile["LastName"];
accessProfile["StartDate"] = employeeProfile["StartDate"];
accessProfile["DueDate"] = **employeeProfile["StartDate"] + newHireTaskTemplate[DueinDays"]???**

Solution

  • Replace the code below

    accessProfile["DueDate"] = **employeeProfile["StartDate"] + newHireTaskTemplate[DueinDays"]???**
    

    with

    if (employeeProfile["StartDate"] != null && newHireTaskTemplate["DueinDays"]!=null)
    {
        accessProfile["DueDate"] = DateTime.Parse(employeeProfile["StartDate"].ToString()).AddDays(int.Parse(newHireTaskTemplate["DueinDays"].ToString()));
    }