Add actual Hours in TimeSheet using CSOM Project Online

Add actual Hours  in TimeSheet using CSOM Project Online

Add actual Hours in TimeSheet using CSOM Project Online. In this post, you learn to Add actual Hours in TimeSheet using CSOM Project Online. A task assigns for the resource for a specific time period the corresponding and corresponding Lines, Actual are created. If you need to Add actual hours in timesheet using PSI, follow my post Update Timesheet Using PSI

If you don’t know to add references in your solutions then follow the post below

Add  references in Solution

[AdSense-A]

public static void AddActualToTaskTimeSheet()
        {
            try
            {
                using (ProjectContext ProjectCont = new ProjectContext(PWA))//PWA Url
                {
                    SecureString passWord2 = new SecureString();
                    foreach (char c in Passwords.ToCharArray()) passWord2.AppendChar(c);
                    ProjectCont.Credentials = new SharePointOnlineCredentials(UserName, passWord2);
                    ProjectCont.Load(ProjectCont.Projects,
                    c => c.IncludeWithDefaultProperties());
                    ProjectCont.ExecuteQuery();

                    ProjectCont.Load(ProjectCont.TimeSheetPeriods, c => c.Where(p => p.Start <= DateTime.Now && p.End >= DateTime.Now).
                        IncludeWithDefaultProperties(p => p.TimeSheet,
                                                     p => p.TimeSheet.Lines.Where(l => l.LineClass == TimeSheetLineClass.StandardLine).
                        IncludeWithDefaultProperties(l => l.Assignment,
                                                           l => l.Assignment.Task,
                                                           l => l.Work)));

                    ProjectCont.ExecuteQuery();

                    var myPeriod = ProjectCont.TimeSheetPeriods.FirstOrDefault();

                    if (myPeriod == null)
                        throw new Exception("Create the periods");


                    var line = myPeriod.TimeSheet.Lines;// FirstOrDefault();


                    foreach (var item in line)
                    {
                        if (item.TaskName.Equals("Task4"))
                        {
                            Console.WriteLine(item.TaskName);
                            TimeSheetWorkCreationInformation workCreation = new TimeSheetWorkCreationInformation
                            {

                                ActualWork = string.Format("{0}h", 3),
                                Start = DateTime.Now.AddDays(-2),
                                End = DateTime.Now,
                                Comment = "pdate By Project Server Code",
                                NonBillableOvertimeWork = "0",
                                NonBillableWork = "0",
                                OvertimeWork = "0",
                                PlannedWork = "0h",

                            };
                            if (myPeriod.TimeSheet.Status == TimeSheetStatus.Approved ||
                                myPeriod.TimeSheet.Status == TimeSheetStatus.Submitted ||
                                myPeriod.TimeSheet.Status == TimeSheetStatus.Rejected)
                            {
                                myPeriod.TimeSheet.Recall(); // Recall the timesheet if Timesheet status is Approved or Submitted or Rejected
                            }
                            item.Work.Add(workCreation);

                            myPeriod.TimeSheet.Submit("Close");
                        }
                    }
                    ProjectCont.ExecuteQuery();


                }
            }

            catch (Exception ex)
            {
            }
        }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!