/// <summary> /// Backups and exports lists /// </summary> /// <param name="clientContextWeb">client context url</param> /// <param name="backupListTarget">list that is exported and deleted</param> /// <param name="backupListSource">list that needs to be backedup</param> /// <param name="pipelineBackupDocLib">doc library for backup of excel file</param> public BackupHelper(string clientContextWeb, string backupListTarget, string backupListSource, string pipelineBackupDocLib) { try { using (var clientContext = new ClientContext(clientContextWeb)) { try { Web web = clientContext.Web; List oldList = web.Lists.GetByTitle(backupListTarget); List newList = web.Lists.GetByTitle(backupListSource); // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" // so that it grabs all list items, regardless of the folder they are in. CamlQuery query = CamlQuery.CreateAllItemsQuery(2000); CamlQuery newquery = CamlQuery.CreateAllItemsQuery(2000); ListItemCollection oldItems = oldList.GetItems(query); ListItemCollection newItems = newList.GetItems(newquery); int counter = 0; var listFields = newList.Fields; clientContext.Load(listFields, fields => fields.Include(field => field.Title, field => field.InternalName, field => field.ReadOnlyField)); clientContext.Load(oldItems); clientContext.Load(newItems); clientContext.ExecuteQuery(); // Retrieve all items in the ListItemCollection from List.GetItems(Query). DataTable dt = new DataTable("ExportData"); foreach (Field f in listFields) { try { // if (Array.IndexOf(stringArray, f.Title) >= 0) // { if (Array.IndexOf(badTitleArray, f.InternalName) < 0 && !f.ReadOnlyField) { dt.Columns.Add(f.Title); } } catch (Exception ex) { log.Error("error creating table", ex); } } foreach (ListItem item in oldItems) { DataRow dr = dt.NewRow(); foreach (Field f in listFields) { if (Array.IndexOf(badTitleArray, f.InternalName) < 0 && !f.ReadOnlyField) { try { dr[f.Title] = item[f.InternalName]; } catch (Exception ex) { log.Debug("found a problem with field"); } } } dt.Rows.Add(dr); ///WORKING DELETE OBJECTS oldItems.GetById(item.Id).DeleteObject(); if (counter > 50) { try { clientContext.ExecuteQuery(); } catch (Exception ex) { log.Error("caught and exception", ex); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("We had an issue:{0}", ex.ToString())); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Connectivity was lost please try again"); throw ex; } } else { counter++; } } //finish up the final documents counter = 0; try { clientContext.ExecuteQuery(); } catch (Exception ex) { log.Error("caught and exception", ex); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("We had an issue:{0}", ex.ToString())); Console.WriteLine("Connectivity was lost please try again"); throw ex; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Items are all deleted we are about to save our file"); /// save excel file XLWorkbook wb = new XLWorkbook(); wb.Worksheets.Add(dt, "WorksheetName"); string timestamp = DateTime.Now.ToString("yyyyMMddhhmm"); string fileName = "backup" + timestamp + ".xlsx"; wb.SaveAs(fileName); using (var fs = new FileStream(fileName, FileMode.Open)) { var fi = new FileInfo(fileName); var list = clientContext.Web.Lists.GetByTitle(pipelineBackupDocLib); clientContext.Load(list.RootFolder); clientContext.ExecuteQuery(); var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name); Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("The file was successfully saved and we are about to copy lists"); foreach (ListItem item in newItems) { var itemCreateInfo = new ListItemCreationInformation(); var newListItem = oldList.AddItem(itemCreateInfo); StringBuilder sbLog = new StringBuilder(); foreach (Field f in listFields) { // if (Array.IndexOf(stringArray, f.Title) >= 0) // { if (Array.IndexOf(badTitleArray, f.InternalName) < 0 && !f.ReadOnlyField) { if (f.InternalName.ToLower() == "title") { sbLog.Append(string.Format("Field Name:{0}", f.InternalName)); } newListItem[f.InternalName] = item[f.InternalName]; } //} } newListItem.Update(); if (counter > 50) { try { clientContext.ExecuteQuery(); } catch (Exception ex) { log.Error("caught and exception", ex); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("We had an issue:{0}", ex.ToString())); Console.WriteLine("Connectivity was lost please try again"); // throw ex; } } else { counter++; //log.Debug(string.Format("added item:{0}", sbLog.ToString())); } } //finish up the final documents counter = 0; try { clientContext.ExecuteQuery(); } catch (Exception ex) { log.Error("caught and exception", ex); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("We had an issue:{0}", ex.ToString())); Console.WriteLine("Connectivity was lost please try again"); // throw ex; } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("We had an issue Please record, exit and try again:{0}", ex.ToString())); Console.ReadLine(); } } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Client context was lost: Pleasse record and press enter"); log.Error("Client context was lost", ex); Console.ReadLine(); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully completed Backup We will start the pipeline update"); Console.ForegroundColor = ConsoleColor.White; //Console.ReadLine(); }