public static string updateAndCloseSurveyList(string spSiteUrl, IDictionary <string, string> cookies, string ListName, SurveyEntries entries) { try { using (ClientContext context = new ClientContext(spSiteUrl)) { context.ExecutingWebRequest += delegate(object sender, WebRequestEventArgs e) { e.WebRequestExecutor.WebRequest.CookieContainer = new CookieContainer(); foreach (KeyValuePair <string, string> KvpCookie in cookies) { Cookie cookie = new Cookie(KvpCookie.Key, KvpCookie.Value); e.WebRequestExecutor.WebRequest.CookieContainer.Add(new Uri(spSiteUrl), cookie); } }; string sDetractorString = "", sPassiveString = "", sPromoterString = "", sEmotionScore = "", sSurveyName = ""; foreach (Entry entry in entries.GetEntries()) { sDetractorString = entry.DetractorString; sPassiveString = entry.PassiveString; sPromoterString = entry.PromoterString; sEmotionScore = entry.emotionScore; sSurveyName = entry.Title; } var web = context.Web; context.Load(web); context.ExecuteQuery(); List oList = context.Web.Lists.GetByTitle(ListName); context.Load(oList); context.ExecuteQuery(); CamlQuery query = new CamlQuery(); query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + sSurveyName + "</Value></Eq></Where><OrderBy><FieldRef Name='SurveyStatus' Ascending='False' /></OrderBy></Query><ViewFields><FieldRef Name='Promoter' /><FieldRef Name='Passive' /><FieldRef Name='Detractor' /><FieldRef Name='EmotionScore' /><FieldRef Name='NPSScore' /><FieldRef Name='Title' /><FieldRef Name='SurveyStatus' /></ViewFields><QueryOptions /></View>"; ListItemCollection oListItem = oList.GetItems(query); context.Load(oListItem); context.ExecuteQuery(); ListItem oItem = oListItem[0]; if (oListItem.Count > 0) { RootEmotion jsonEmotionObject = JsonConvert.DeserializeObject <RootEmotion>(sEmotionScore); oItem["Detractor"] = sDetractorString; oItem["Passive"] = sPassiveString; oItem["Promoter"] = sPromoterString; oItem["EmotionScore"] = sEmotionScore; oItem["NetEmotionScore"] = (null == jsonEmotionObject.NetScore ? float.Parse("0.00") : float.Parse((string)jsonEmotionObject.NetScore, CultureInfo.InvariantCulture.NumberFormat)); oItem["SurveyStatus"] = "Closed"; oItem.Update(); context.ExecuteQuery(); return("success"); } else { return("failed"); } } } catch (Exception e) { return("failed"); } }
public static string AddListItemsInBatch(string spSiteUrl, IDictionary <string, string> cookies, string ListName, SurveyEntries entries) { try { using (ClientContext context = new ClientContext(spSiteUrl)) { context.ExecutingWebRequest += delegate(object sender, WebRequestEventArgs e) { e.WebRequestExecutor.WebRequest.CookieContainer = new CookieContainer(); foreach (KeyValuePair <string, string> KvpCookie in cookies) { Cookie cookie = new Cookie(KvpCookie.Key, KvpCookie.Value); e.WebRequestExecutor.WebRequest.CookieContainer.Add(new Uri(spSiteUrl), cookie); } }; List list = context.Web.Lists.GetByTitle(ListName); foreach (Entry entry in entries.GetEntries()) { ListItemCreationInformation info = new ListItemCreationInformation(); ListItem listItem = list.AddItem(info); listItem["Title"] = entry.Title; listItem["Promoter"] = entry.PromoterString; listItem["Detractor"] = entry.DetractorString; listItem["Passive"] = entry.PassiveString; var Survey = new FieldLookupValue(); Survey.LookupId = Convert.ToInt32(entry.SurveyLookupIdString); listItem["Survey"] = Survey; listItem["EmotionScore"] = entry.emotionScore; listItem.Update(); } context.ExecuteQuery(); } return("success"); } catch (Exception e) { return("failed"); } }