Пример #1
0
        /// <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");
                }
            }
        }
Пример #2
0
        /// <summary>
        /// upload repports to sharepoint library acording to config file
        /// </summary>
        /// <returns></returns>
        public static async Task UploadExportsToSpLibraries()
        {
            Log.Info($"Uploading exports to SharePoint's library");

            FormDatasRespViewModel formData = null;
            HttpResponseMessage    response;
            MarkDataReqViewModel   dataToMark;
            string formId;

            foreach (var formToSpLibrary in Config.FormsToSpLibraries)
            {
                string marker = KfApiManager.CreateKFMarker(formToSpLibrary.FormId, formToSpLibrary.SpLibraryId);
                formId     = formToSpLibrary.FormId;
                dataToMark = new MarkDataReqViewModel();

                Log.Debug($"-Processing form : {formId}");
                response = await HttpClient.GetAsync($"{Config.KizeoConfig.Url}/rest/v3/forms/{formId}/data/unread/{marker}/50?includeupdated");

                if (response.IsSuccessStatusCode)
                {
                    Log.Debug($"-Loading Sharepoint's library");
                    formToSpLibrary.SpLibrary = SpManager.LoadSpLibrary(formToSpLibrary);

                    formData = await response.Content.ReadAsAsync <FormDatasRespViewModel>();

                    if (formData.Data == null)
                    {
                        TOOLS.LogErrorAndExitProgram($"Can not find a form for id {formId}, check if this is a valid id.");
                    }

                    Log.Debug($"{formData.Data.Count} Data retrieved successfully from form");

                    var allSpPaths = SpManager.GetAllLibraryFolders(formToSpLibrary.SpLibrary, formToSpLibrary.SpWebSiteUrl);

                    foreach (var data in formData.Data)
                    {
                        Log.Debug($"-Processing data : {data.Id}");
                        var allExportPaths = await GetAllExportsPath(data, formToSpLibrary);

                        Log.Warn("Creating All Folders hierarchy");
                        foreach (var path in allExportPaths)
                        {
                            if (!string.IsNullOrEmpty(path) && !string.IsNullOrWhiteSpace(path) && !allSpPaths.Contains(path))
                            {
                                Log.Warn($"Creating path : {path}");
                                TOOLS.CreateSpPath(Context, path, formToSpLibrary.SpLibrary);
                                allSpPaths.Add(path);
                            }
                        }

                        Log.Debug($"--Processing data : {data.Id}");

                        SpManager.RunExport("PDF", $"rest/v3/forms/{formId}/data/{data.Id}/pdf", formToSpLibrary.ToStandardPdf, formToSpLibrary, data, formToSpLibrary.StandardPdfPath);
                        SpManager.RunExport("Excel", $"rest/v3/forms/{formId}/data/multiple/excel", formToSpLibrary.ToExcelList, formToSpLibrary, data, formToSpLibrary.ExcelListPath, new string[] { data.Id });
                        SpManager.RunExport("CSV", $"rest/v3/forms/{formId}/data/multiple/csv", formToSpLibrary.ToCsv, formToSpLibrary, data, formToSpLibrary.CsvPath, new string[] { data.Id });
                        SpManager.RunExport("CSV_Custom", $"rest/v3/forms/{formId}/data/multiple/csv_custom", formToSpLibrary.ToCsvCustom, formToSpLibrary, data, formToSpLibrary.CsvCustomPath, new string[] { data.Id });
                        SpManager.RunExport("Excel_Custom", $"rest/v3/forms/{formId}/data/multiple/excel_custom", formToSpLibrary.ToExcelListCustom, formToSpLibrary, data, formToSpLibrary.ExcelListCustomPath, new string[] { data.Id });

                        if (formToSpLibrary.Exports != null && formToSpLibrary.Exports.Count > 0)
                        {
                            foreach (var export in formToSpLibrary.Exports)
                            {
                                Log.Debug($"---Processing export : {export.Id}");

                                SpManager.RunExport("Initial Type", $"rest/v3/forms/{formId}/data/{data.Id}/exports/{export.Id}", export.ToInitialType, formToSpLibrary, data, export.initialTypePath);
                                SpManager.RunExport("Pdf Type", $"rest/v3/forms/{formId}/data/{data.Id}/exports/{export.Id}/pdf", export.ToPdf, formToSpLibrary, data, export.PdfPath);
                            }
                        }

                        dataToMark.Ids.Add(data.Id);
                    }

                    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");
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Add and send item to Sharepointlist including Media
        /// </summary>
        /// <param name="spList">guid of sharepoint list</param>
        /// <param name="item">the item</param>
        /// <param name="data">data that holds dataId and formId</param>
        /// <param name="dataToMark"></param>
        /// <param name="itemUpdated">In case of update, use this item</param>
        /// <returns></returns>
        public async Task <ListItem> AddItemToList(List spList, List <DataMapping> dataMappings, FormData data, MarkDataReqViewModel dataToMark, List <DataMapping> uniqueDataMappings, ListItemCollection allItems)
        {
            bool containsArray = false;

            Log.Debug($"Processing data : {data.Id}");
            List <ListItem> toAdd = new List <ListItem>();
            bool            shouldFulfillOtherLines = false;
            int             toCreate = -1;

            ListItem item = spList.AddItem(new ListItemCreationInformation());

            var r = await NeedTobeUpdated(dataMappings, data, spList, uniqueDataMappings);

            if (r != null)
            {
                item = r;
            }
            foreach (var dataMapping in dataMappings)
            {
                string[] columnValues = await KfApiManager.TransformText(data.FormID, data.Id, dataMapping.KfColumnSelector);

                TOOLS.ConvertToCorrectTypeAndSet(item, dataMapping, columnValues.First());
                if (columnValues.Length > 1) // if there's at least one row to add
                {
                    shouldFulfillOtherLines = true;
                    if (toCreate.Equals(-1))
                    {
                        toCreate = columnValues.Length - 1;
                    }

                    containsArray = true;
                    int cvc = 0;
                    foreach (string columnValue in columnValues)
                    {
                        if (columnValue != columnValues.First())
                        {
                            if (toCreate > 0)
                            {
                                var tmp_item = spList.AddItem(new ListItemCreationInformation());

                                foreach (var field in dataMappings)
                                {
                                    if (item.FieldValues.ContainsKey(field.SpColumnId))
                                    {
                                        tmp_item[field.SpColumnId] = item[field.SpColumnId];
                                    }
                                }
                                TOOLS.ConvertToCorrectTypeAndSet(tmp_item, dataMapping, columnValue);
                                toAdd.Add(tmp_item);
                                toCreate--;
                            }
                            else
                            {
                                TOOLS.ConvertToCorrectTypeAndSet(toAdd[cvc++], dataMapping, columnValue);
                            }
                        }
                    }
                }

                else if (shouldFulfillOtherLines)
                {
                    foreach (var line in toAdd)
                    {
                        line[dataMapping.SpColumnId] = columnValues.First();
                    }
                }
            }
            toAdd.Insert(0, item);
            if (containsArray)
            {
                RemoveDuplicated(ref toAdd, dataMappings, spList, uniqueDataMappings, allItems);
            }
            try
            {
                lock (locky)
                {
                    foreach (var add in toAdd)
                    {
                        add.Update();
                    }
                    Context.ExecuteQuery();
                    dataToMark.Ids.Add(data.Id);
                }

                var x        = $"{KfApiManager.KfApiUrl}/rest/v3/forms/{data.FormID}/data/{data.Id}/all_medias";
                var response = await KfApiManager.HttpClient.GetAsync(x);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    using (var ms = new MemoryStream())
                    {
                        Log.Debug($"processing media for data {data.Id}");
                        await response.Content.CopyToAsync(ms);

                        ms.Seek(0, SeekOrigin.Begin);
                        AttachmentCreationInformation pjInfo = new AttachmentCreationInformation();
                        pjInfo.ContentStream = ms;
                        pjInfo.FileName      = response.Content.Headers.ContentDisposition.FileName;

                        Attachment pj = toAdd.Last().AttachmentFiles.Add(pjInfo);

                        lock (locky)
                        {
                            Context.Load(pj);
                            Context.ExecuteQuery();
                        }
                    }
                }

                /*   foreach (var item in items)
                 * {
                 *     item.DeleteObject();
                 * }*/
                Context.ExecuteQuery();
                Log.Debug($"Data {data.Id} sent to Sharepoint successefully");
            }
            catch (Exception e)
            {
                throw;
            }

            return(null);
        }