static void checkEngagement(string proj_uid, string res_uid = "4d6e3553-5ab1-e411-9a07-00155d509515", string alloc_uid = "zd6e3553-5ab1-e411-9a07-00155d509515")
        {
            res_uid = "8a8b380f-10ba-e411-ab5d-00155da4340f"; //bt
            pubProj = projContext.Projects.GetByGuid(new Guid(proj_uid));
            projContext.Load(pubProj);
            projContext.ExecuteQuery();

            draftProj = pubProj.CheckOut();

            projContext.Load(draftProj.Engagements);
            projContext.ExecuteQuery();

            ProjectEngagement pEng = null;

            foreach (ProjectEngagement pe in draftProj.Engagements)
            {
                projContext.Load(pe);
                projContext.Load(pe.Resource);
                projContext.ExecuteQuery();
                if (pe.Resource.Id.ToString() == res_uid)
                {
                    pEng = pe;
                    res  = pe.Resource;
                    break;
                }
            }
            //ProjectEngagement pEng = draftProj.Engagements.First(e => e.Resource.Id.ToString() == res_uid );
            //projContext.Load(pEng.Resource);
            //projContext.ExecuteQuery();
            //res = pEng.Resource;

            if (pEng == null)
            {
                res = resources.GetByGuid(new Guid(res_uid));
                projContext.Load(res);
                projContext.ExecuteQuery();

                ProjectEngagementCreationInformation peci = new ProjectEngagementCreationInformation();
                peci.Id          = Guid.NewGuid();
                peci.Start       = Convert.ToDateTime("1/1/2019");
                peci.Finish      = Convert.ToDateTime("1/1/2019");
                peci.Resource    = res;
                peci.Work        = "0h";
                peci.Description = "RPM_" + alloc_uid;



                //projContext.Load(draftProj.Engagements);
                //projContext.Load(pubProj.Engagements);
                //projContext.ExecuteQuery();

                //if (calcfrom == "project")
                //    draftProj.UtilizationType = ProjectUtilizationType.ProjectPlan;
                //else
                //    draftProj.UtilizationType = ProjectUtilizationType.ResourceEngagements;

                draftProj.Engagements.Add(peci).Status = EngagementStatus.Proposed;
                draftProj.Engagements.Update();
                pEng = draftProj.Engagements.Last();

                projContext.Load(pEng);
                projContext.Load(pEng.Resource);
                projContext.ExecuteQuery();
            }

            ProjectEngagementTimephasedCollection petpc = pEng.GetTimephased(Convert.ToDateTime("5/1/2020"), Convert.ToDateTime("5/31/2020"), TimeScale.Months, EngagementContourType.Draft);

            projContext.Load(petpc);
            projContext.ExecuteQuery();
            petpc[0].Work = "20h";
            petpc[1].Work = "20h";
            petpc[2].Work = "30h";
            petpc[3].Work = "40h";
            petpc[4].Work = "50h";
            petpc[5].Work = "60h";
            petpc[6].Work = "70h";
            petpc[7].Work = "80h";

            pEng.Status = EngagementStatus.Reproposed;
            draftProj.Engagements.Update();

            draftProj.CheckIn(false);
            QueueJob qJob1    = projContext.Projects.Update();
            JobState jobState = projContext.WaitForQueue(qJob1, timeoutSeconds);


            {
                //approve proposed request
                projContext.Load(res.Engagements);
                projContext.ExecuteQuery();

                ResourceEngagement eng = res.Engagements.GetById(pEng.Id.ToString());
                projContext.Load(eng);
                projContext.ExecuteQuery();  //Too many resources: 4205. You cannot load dependent objects for more than 1000 resources. Use a filter to restrict your query

                eng.Status = EngagementStatus.Approved;
                res.Engagements.Update();

                QueueJob qJob = projContext.Projects.Update();
                jobState = projContext.WaitForQueue(qJob, timeoutSeconds);
            }
        }
        static void SyncEngagments()
        {
            ProjectEngagement pEng     = null;
            string            res_uid  = "";
            string            proj_uid = "";
            string            err      = "";
            string            sql      = "exec [MCG_RM_GetPendingUpdates_engagements]";

            DataSet        ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);

            da.Fill(ds);

            foreach (DataRow r in ds.Tables[0].Rows)
            {
                res_uid  = r["resourceuid"].ToString();
                proj_uid = r["projectuid"].ToString();

                pubProj = projContext.Projects.GetByGuid(new Guid(proj_uid));
                projContext.Load(pubProj);
                projContext.ExecuteQuery();

                draftProj = pubProj.CheckOut();

                projContext.Load(draftProj.Engagements);
                projContext.ExecuteQuery();

                if (!r.IsNull("engagementuid"))
                {
                    pEng = draftProj.Engagements.GetByGuid(new Guid(r["engagementuid"].ToString()));
                }

                if (pEng == null)
                {
                    res = projContext.EnterpriseResources.GetByGuid(new Guid(res_uid));
                    projContext.Load(res);
                    projContext.ExecuteQuery();

                    ProjectEngagementCreationInformation peci = new ProjectEngagementCreationInformation();
                    peci.Id          = Guid.NewGuid();
                    peci.Start       = Convert.ToDateTime(r["start_dt"].ToString());
                    peci.Finish      = Convert.ToDateTime(r["end_dt"].ToString());
                    peci.Resource    = res;
                    peci.Work        = "0h";
                    peci.Description = "RPM_" + r["planuid"].ToString();

                    draftProj.Engagements.Add(peci).Status = EngagementStatus.Proposed;
                    draftProj.Engagements.Update();
                    pEng = draftProj.Engagements.Last();
                    projContext.Load(pEng);
                    projContext.ExecuteQuery();
                }


                DataRow[] rows = ds.Tables[1].Select("resourceuid='" + res_uid + "' and projectuid='" + proj_uid + "'");
                ProjectEngagementTimephasedCollection petpc = pEng.GetTimephased(Convert.ToDateTime(r["start_dt"]), Convert.ToDateTime(r["end_dt"]), TimeScale.Days, EngagementContourType.Draft);

                projContext.Load(petpc);
                projContext.ExecuteQuery();

                foreach (DataRow row in rows)
                {
                    petpc.GetByStart(Convert.ToDateTime(row["timebyday"].ToString())).Work = row["allocationwork"].ToString();
                }
                pEng.Status = EngagementStatus.Reproposed; //this is needed
                draftProj.Engagements.Update();

                draftProj.CheckIn(false);
                //this updates the last TBD engagement in PWA, Approved will remove Eng from PWA
                QueueJob qJob1    = projContext.Projects.Update();
                JobState jobState = projContext.WaitForQueue(qJob1, timeoutSeconds);

                {
                    //approve proposed request
                    if (res == null)
                    {
                        res = projContext.EnterpriseResources.GetByGuid(new Guid(res_uid));
                        projContext.Load(res);
                        projContext.Load(res.Engagements);
                        projContext.ExecuteQuery();
                    }

                    ResourceEngagement eng = res.Engagements.GetById(pEng.Id.ToString());
                    projContext.Load(eng);
                    projContext.ExecuteQuery();  //Too many resources: 4205. You cannot load dependent objects for more than 1000 resources. Use a filter to restrict your query

                    eng.Status = EngagementStatus.Approved;
                    res.Engagements.Update();

                    QueueJob qJob = projContext.Projects.Update();
                    jobState = projContext.WaitForQueue(qJob, timeoutSeconds);
                }
            }
        }
        static void testEngagement_old(DataSet ds)
        {
            string res_uid  = "";
            string proj_uid = "";

            foreach (DataRow r in ds.Tables[1].Rows)
            {
                res_uid  = r["resourceuid"].ToString();
                proj_uid = r["projectuid"].ToString();

                pubProj = projContext.Projects.GetByGuid(new Guid(proj_uid));
                projContext.Load(pubProj);
                projContext.ExecuteQuery();

                draftProj = pubProj.CheckOut();

                projContext.Load(draftProj.Engagements);
                projContext.ExecuteQuery();

                ProjectEngagement pEng = null;
                foreach (ProjectEngagement pe in draftProj.Engagements)
                {
                    projContext.Load(pe);
                    projContext.Load(pe.Resource);
                    projContext.ExecuteQuery();
                    if (pe.Resource.Id.ToString().ToUpper() == res_uid.ToUpper())
                    {
                        pEng = pe;
                        res  = pe.Resource;
                        break;
                    }
                }

                if (pEng == null)
                {
                    res = resources.GetByGuid(new Guid(res_uid));
                    projContext.Load(res);
                    projContext.ExecuteQuery();

                    ProjectEngagementCreationInformation peci = new ProjectEngagementCreationInformation();
                    peci.Id          = Guid.NewGuid();
                    peci.Start       = Convert.ToDateTime("1/1/2019");
                    peci.Finish      = Convert.ToDateTime("1/1/2019");
                    peci.Resource    = res;
                    peci.Work        = "0h";
                    peci.Description = "RPM_" + "alloc_uid";

                    draftProj.Engagements.Add(peci).Status = EngagementStatus.Proposed;
                    draftProj.Engagements.Update();
                    pEng = draftProj.Engagements.Last();

                    projContext.Load(pEng);
                    projContext.Load(pEng.Resource);
                    projContext.ExecuteQuery();
                }


                DataRow[] rows = ds.Tables[2].Select("resourceuid='" + res_uid + "' and projectuid='" + proj_uid + "'");
                ProjectEngagementTimephasedCollection petpc = pEng.GetTimephased(Convert.ToDateTime(r["start_dt"]), Convert.ToDateTime(r["end_dt"]), TimeScale.Days, EngagementContourType.Draft);

                //works
                //ProjectEngagementTimephasedCollection petpc = pEng.GetTimephased(Convert.ToDateTime("10/1/2019"), Convert.ToDateTime("10/31/2019"), TimeScale.Months, EngagementContourType.Draft);

                //ProjectEngagementTimephasedCollection petpc = pEng.GetTimephased(Convert.ToDateTime("10/1/2019"), Convert.ToDateTime("12/31/2019"), TimeScale.Days, EngagementContourType.Draft);

                projContext.Load(petpc);
                projContext.ExecuteQuery();

                //petpc.GetByStart(Convert.ToDateTime("10/1/2019")).Work = "1";
                //petpc.GetByStart(Convert.ToDateTime("10/2/2019")).Work = "2";
                //petpc.GetByStart(Convert.ToDateTime("10/3/2019")).Work = "3";
                //petpc.GetByStart(Convert.ToDateTime("10/4/2019")).Work = "0";
                //petpc[3].Work = "4";
                //DateTime dt = petpc[2].Start;

                //petpc[0].Work = "100h";
                //petpc.GetByStart(Convert.ToDateTime("10/1/2019")).Work = "50h";
                //draftProj.Engagements.Update();
                ProjectEngagementTimephasedPeriod petpP = null;
                foreach (DataRow row in rows)
                {
                    petpc.GetByStart(Convert.ToDateTime(row["timebyday"].ToString())).Work = row["allocationwork"].ToString();

                    //string dt = row["timebyday"].ToString();
                    //petpP = petpc.GetByStart(Convert.ToDateTime(row["timebyday"]));
                    //projContext.Load(petpP);
                    //projContext.ExecuteQuery();
                    //petpP.Work = row["allocationwork"].ToString();

                    //ProjectEngagementTimephasedCollection petpc = pEng.GetTimephased(Convert.ToDateTime(row["timebyday"]), Convert.ToDateTime(row["timebyday"]), TimeScale.Days, EngagementContourType.Draft);
                    //projContext.Load(petpc);
                    //projContext.ExecuteQuery();
                    //petpc[0].Work = row["allocationwork"].ToString();
                    //draftProj.Engagements.Update();
                }

                //draftProj.Engagements.Update();
                pEng.Status = EngagementStatus.Reproposed; //this is needed
                draftProj.Engagements.Update();

                draftProj.CheckIn(false);
                //this updates the last TBD engagement in PWA, Approved will remove Eng from PWA
                QueueJob qJob1    = projContext.Projects.Update();
                JobState jobState = projContext.WaitForQueue(qJob1, timeoutSeconds);

                {
                    //approve proposed request
                    projContext.Load(res.Engagements);
                    projContext.ExecuteQuery();

                    ResourceEngagement eng = res.Engagements.GetById(pEng.Id.ToString());
                    projContext.Load(eng);
                    projContext.ExecuteQuery();  //Too many resources: 4205. You cannot load dependent objects for more than 1000 resources. Use a filter to restrict your query

                    eng.Status = EngagementStatus.Approved;
                    res.Engagements.Update();

                    QueueJob qJob = projContext.Projects.Update();
                    jobState = projContext.WaitForQueue(qJob, timeoutSeconds);
                }
            }
        }