Пример #1
0
        static void Main(string[] args)
        {
            var siteUrl = ConfigurationManager.AppSettings["site"];

            using (var context = new ClientContext(siteUrl))
            {
                Web          site     = context.Web;
                var          pwd      = ConfigurationManager.AppSettings["password"];
                SecureString passWord = new SecureString();
                foreach (char c in pwd)
                {
                    passWord.AppendChar(c);
                }
                context.Credentials = new SharePointOnlineCredentials(ConfigurationManager.AppSettings["username"], passWord);
                List      targetList = site.Lists.GetByTitle(ConfigurationManager.AppSettings["list"]);
                CamlQuery query      = new CamlQuery();
                query.ViewXml = @"<View><Query><OrderBy><FieldRef Name='Date' Ascending='false'/></OrderBy><Where><And><Geq><FieldRef Name='Date' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + new DateTime(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month, 1).ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Value></Geq><Leq><FieldRef Name='Date' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Value></Leq></And></Where></Query><RowLimit>500</RowLimit></View>";
                ListItemCollection collListItem = targetList.GetItems(query);

                context.Load(collListItem);
                context.ExecuteQuery();
                foreach (var v in collListItem.Reverse())
                {
                    Console.WriteLine(((FieldLookupValue)v["Project_x0020__x002d__x0020_Cust0"]).LookupValue + " --- Date:" + v["Date"] + " --- Hours: " + v["Hours"]);
                }
                //Console.WriteLine("Hours this year: " + collListItem?.Sum(x => (double)x["Hours"]));
                //Console.WriteLine("How many +- hours this year: " + (collListItem?.Sum(x => (double)x["Hours"]) - collListItem?.GroupBy(c => DateTime.Parse(c["Date"].ToString()).Date).Count() * 8));
                var gbm = collListItem.GroupBy(v => DateTime.Parse(v["Date"].ToString()).Month);
                var cmg = gbm.FirstOrDefault(v => v.Key == DateTime.Now.Month);
                Console.WriteLine("Hours this month: " + cmg?.Sum(x => (double)x["Hours"]));
                Console.WriteLine("How many +- hours this month: " + (cmg?.Sum(x => (double)x["Hours"]) - cmg?.GroupBy(c => DateTime.Parse(c["Date"].ToString()).Date).Count() * 8));
                var gbw = collListItem.GroupBy(v => DateTime.Parse(v["Date"].ToString()).AddDays(-(int)DateTime.Parse(v["Date"].ToString()).DayOfWeek + 1).Date);
                var cwg = gbw.FirstOrDefault(x => x.Key == DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek + 1).Date);
                Console.WriteLine("Hours this week: " + cwg?.Sum(x => (double)x["Hours"]));
                Console.WriteLine("How many +- hours: " + (cwg?.Sum(x => (double)x["Hours"]) - cwg?.GroupBy(c => DateTime.Parse(c["Date"].ToString()).Date).Count() * 8));
                foreach (var targetListItem in collListItem.GroupBy(x => ((FieldLookupValue)x["Project_x0020__x002d__x0020_Cust0"]).LookupId))
                {
                    Console.WriteLine(((FieldLookupValue)targetListItem?.FirstOrDefault()?["Project_x0020__x002d__x0020_Cust0"])?.LookupValue + " = " + ((FieldLookupValue)targetListItem?.FirstOrDefault()?["Project_x0020__x002d__x0020_Cust0"])?.LookupId);
                }
                var ht = HoursToday();

                Console.WriteLine("How many hours have you worked today?");
                Console.WriteLine("From: " + ht.StartTime);
                Console.WriteLine("To: " + ht.EndTime);
                var hoursToday = ConsoleReadLineWithDefault(ht.Hours);
                if (hoursToday != ht.Hours)
                {
                    hoursToday = hoursToday.Replace(",", ".");
                    ht.EndTime = ht.StartTime.AddHours(double.Parse(hoursToday, CultureInfo.GetCultureInfo("en-US")));
                    Console.WriteLine("New To: " + ht.EndTime);
                }
                Console.WriteLine("Which project id have you worked on?");
                var projectId = ConsoleReadLineWithDefault(((FieldLookupValue)collListItem?.FirstOrDefault()?["Project_x0020__x002d__x0020_Cust0"])?.LookupId.ToString());
                Console.WriteLine("Write a comment for the time registration?");
                var comment = Console.ReadLine();

                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem newItem  = targetList.AddItem(itemCreateInfo);
                var      usaHours = 8;
                if (TimeZoneInfo.Local.IsDaylightSavingTime(DateTime.Now))
                {
                    usaHours = 7;
                }
                newItem["Date"]  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, usaHours, 0, 0, DateTimeKind.Utc).ToString("yyyy-MM-ddTHH:mm:ssZ");
                newItem["Hours"] = hoursToday.Replace(",", ".");
                newItem["Project_x0020__x002d__x0020_Cust0"] = projectId;
                newItem["Comments"] = comment;
                //newItem["From"] = ht.StartTime.ToString("MM-dd-yyyy HH:mm"); Time Format is weird, maybe American
                //newItem["To"] = ht.EndTime.ToString("MM-dd-yyyy HH:mm");
                newItem["Person2"] = ((FieldLookupValue)collListItem?.FirstOrDefault()?["Person2"])?.LookupId.ToString();
                newItem.Update();

                context.ExecuteQuery();
            }
        }