/// <summary> /// send file to sp library /// </summary> /// <param name="ms">the memory stream holding the export</param> /// <param name="spLibrary">Guid of spLibrary</param> /// <param name="filePath">the file path</param> /// <param name="fileName_">the fileName</param> private void SendPeriodicExportToSpLibrary(MemoryStream ms, List spLibrary, string filePath, string fileName_) { FileCreationInformation fcInfo = new FileCreationInformation(); fcInfo.Url = Path.Combine(filePath, fileName_); fcInfo.Overwrite = true; fcInfo.Content = ms.ToArray(); Log.Debug("-----Configuring file destination in Sharepoint"); Log.Debug($"-----File url : {fcInfo.Url}"); lock (locky) { try { Microsoft.SharePoint.Client.File uploadedFile = spLibrary.RootFolder.Files.Add(fcInfo); uploadedFile.ListItemAllFields.Update(); uploadedFile.Update(); Context.ExecuteQuery(); Log.Debug($"File : {fcInfo.Url} sent to splibrary successfully"); } catch (Exception Ex) { TOOLS.LogErrorwithoutExitProgram(Ex.Message + $"\n" + " filepath : error while uploading file :" + fcInfo.Url + "\n Stack Trace : " + Ex.StackTrace); } } }
/// <summary> /// send File to Sharepoint /// </summary> /// <param name="ms">the memory stream holding the data</param> /// <param name="formToSpLibrary"> Form to sp library to get config data </param> /// <param name="data"> The data that holds the dataId and the formId</param> /// <param name="metadatas">collection<Datamaping> tha holds the metadata</param> /// <param name="filePath">the filePath</param> /// <param name="fileName_">the fileName</param> public void SendToSpLibrary(MemoryStream ms, List formsLibrary, List <DataMapping> metadatas, FormData data, string filePath, string fileName_) { FileCreationInformation fcInfo = new FileCreationInformation(); fcInfo.Url = Path.Combine(filePath, fileName_); fcInfo.Overwrite = true; fcInfo.Content = ms.ToArray(); Log.Debug("-----Configuring file destination in Sharepoint"); Log.Debug($"-----File url : {fcInfo.Url}"); lock (locky) { try { Microsoft.SharePoint.Client.File uploadedFile = formsLibrary.RootFolder.Files.Add(fcInfo); if (metadatas.Count != 0) { FillFileMetaDatas(metadatas, uploadedFile, data).Wait(); } uploadedFile.ListItemAllFields.Update(); uploadedFile.Update(); Context.ExecuteQuery(); Log.Debug($"File : {fcInfo.Url} sent to splibrary successfully"); } catch (Exception Ex) { TOOLS.LogErrorwithoutExitProgram(Ex.Message + $"\n" + " filepath : error while uploading file :" + fcInfo.Url); TOOLS.LogErrorwithoutExitProgram("This error appears when an illegal character is detected in file path: /, \\"); } } }
/// <summary> /// Fill kizeo Forms External list with SharePoint data acording to the Config file /// </summary> /// <returns></returns> public static async Task FillKfExtListsFromSp() { HttpResponseMessage response; Log.Info("Updating KF's external lists from Sharepoint's lists"); foreach (var spListToExtList in Config.SpListsToExtLists) { Log.Debug($"Loading Sharepoint's list: {spListToExtList.SpListId}"); var spList = SpManager.LoadSpList(spListToExtList.SpListId); ListItemCollection items = SpManager.LoadListItems(spList); Log.Debug("Sharepoint's list Succesfully Loaded"); Log.Debug($"Loading KF's extrenal list: {spListToExtList.ExListId}"); response = await HttpClient.GetAsync($"{Config.KizeoConfig.Url}/rest/v3/lists/{spListToExtList.ExListId}"); if (response.IsSuccessStatusCode) { GetExtListRespViewModel kfExtList = await response.Content.ReadAsAsync <GetExtListRespViewModel>(); if (kfExtList.ExternalList == null) { TOOLS.LogErrorAndExitProgram($"Can not find an externalList with for id {spListToExtList.ExListId}, please check if this is a valid id."); } Log.Debug("KF's list successfully loaded"); List <string> linesToAdd = new List <string>(); foreach (var item in items) { Log.Debug($"Processing item : {item["Title"]}"); linesToAdd.Add(SpManager.TransformSharepointText(spListToExtList.DataSchema, item)); } response = await HttpClient.PutAsJsonAsync($"{Config.KizeoConfig.Url}/rest/v3/lists/{spListToExtList.ExListId}", new { items = linesToAdd }); if (!response.IsSuccessStatusCode) { Log.Error("Error when updating external list with data :"); foreach (var line in linesToAdd) { TOOLS.LogErrorwithoutExitProgram("\n" + line); } TOOLS.LogErrorAndExitProgram($"Error when updating external list {spListToExtList.ExListId} "); } else { Log.Info($"External list {spListToExtList.ExListId} updated successfully"); } } } }
/// <summary> /// this methode converts to the correct type /// </summary> /// <param name="item"></param> /// <param name="dataMapping"></param> /// <param name="columnValue"></param> /// <returns></returns> public static ListItem ConvertToCorrectTypeAndSet(ListItem item, DataMapping dataMapping, string columnValue) { if (dataMapping.SpecialType == "Date") { if (DateTime.TryParse(columnValue, out DateTime date)) { item[dataMapping.SpColumnId] = DateTime.ParseExact(columnValue, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);; } else { TOOLS.LogErrorwithoutExitProgram($"Error Parsing date value : {columnValue}"); item[dataMapping.SpColumnId] = ""; } } else { if (columnValue != "") { item[dataMapping.SpColumnId] = columnValue; } } return(item); }
/// <summary> /// Fill Sharepoint list from kizeo forms data acording to the config file /// </summary> /// <returns></returns> public static async Task FillSpListsFromKfData() { Log.Info("Filling Sharepoint's lists from KF's data"); FormDatasRespViewModel formData = null; HttpResponseMessage response; MarkDataReqViewModel dataToMark = new MarkDataReqViewModel(); foreach (var formToSpList in Config.FormsToSpLists) { string marker = KfApiManager.CreateKFMarker(formToSpList.FormId, formToSpList.SpListId); string formId = formToSpList.FormId; Log.Debug($"Processing form : {formId}"); if (formToSpList.DataMapping != null) { response = await HttpClient.GetAsync($"{Config.KizeoConfig.Url}/rest/v3/forms/{formId}/data/unread/{marker}/50?includeupdated"); if (response.IsSuccessStatusCode) { formData = await response.Content.ReadAsAsync <FormDatasRespViewModel>(); if (formData.Data == null) { TOOLS.LogErrorAndExitProgram($"Can not find a form with for id {formId}, please check if this is a valid id."); } Log.Debug($"{formData.Data.Count} data retrieved successfully from form."); Log.Debug("Loading Sharepoint's list"); var spList = SpManager.LoadSpList(formToSpList.SpListId); ListItemCollection allItems = SpManager.getAllListItems(spList); Log.Debug("Sharepoint's list succesfully loaded"); dataToMark = new MarkDataReqViewModel(); foreach (var data in formData.Data) { try { var uniqueColumns = formToSpList.DataMapping.Where(dm => dm.SpecialType == "Unique").ToList(); await SpManager.AddItemToList(spList, formToSpList.DataMapping, data, dataToMark, uniqueColumns, allItems); } catch (ServerException ex) { TOOLS.LogErrorwithoutExitProgram($"Error while sending item {data.Id} from form {data.FormID} to the Sharepoint's list {spList.Id} : " + ex.Message); } } if (dataToMark.Ids.Count > 0) { response = await HttpClient.PostAsJsonAsync($"{Config.KizeoConfig.Url}/rest/v3/forms/{formId}/markasreadbyaction/{marker}", dataToMark); Log.Debug($"{dataToMark.Ids.Count} data marked"); } } } else { TOOLS.LogErrorAndExitProgram("No datamapping was configured, please add a datamapping"); } } }