/// <summary> /// Converts the imported SheetData into KPListItem objects for importing into SP /// The KPListItem is used so we don't need to use the strongly typed objects which /// is problematic since we want to be able to dynamically support any of the entities. /// TODO: consider adding a generic type T to allow strongly-typed objects to be used. /// </summary> /// <param name="dt"></param> /// <param name="kpListColumns"></param> /// <param name="mappingData"></param> /// <returns></returns> private List <KPListItem> ConvertSheetData(DataTable dt, List <string> kpListColumns, MappingData mappingData, ImportStatus importStatus) { string MAPPING_DEFAULT_DELIMITER = "#"; // need a site url regardless to get an entity item to get its fields // we can use the assigned team, but for 'Export' templates we don't // have a team so we'll need to use any team the user has access to List <KPListItem> listItems = new List <KPListItem>(); // get the list column names and data type Dictionary <string, string> fieldInfo = this.spDataAccess.GetEntityFields(mappingData.Team.SiteUrl, mappingData.ListName); int rowCount = 0; try { // enumerate the rows of the data table foreach (DataRow row in dt.Rows) { // create a new item - which represents a row/item/entity KPListItem item = new KPListItem(); // convert each row into a KPListItem foreach (KeyValuePair <string, string> kvPair in mappingData.Mappings) { string value = string.Empty; // mapping.Key is the list column name string listColumn = kvPair.Key; // mapping.Value is the sheet column name string sheetColumn = kvPair.Value; // get the type of field string fieldType = fieldInfo[listColumn]; // does the mapped column contain a default value? // #-prefix flags default values if (kvPair.Value.Contains(MAPPING_DEFAULT_DELIMITER)) { // use the mapped value - remove the token value = sheetColumn.Remove(0, 1); } else { // check the worksheet row[column] for the mapped column value = row[sheetColumn].ToString(); } // create the field with value and data type if (!string.IsNullOrEmpty(value)) { value = KPUtilities.ConvertToSPType(value, fieldType); // if value is null it's an invalid date if (value == null) { importStatus.SkippedColumns.Add(string.Format("{0}: [Invalid Date]", sheetColumn)); } item.Add(listColumn, new KPItem(value, fieldType)); } } // ensure we have columns - but if we have '1' then it's KPTeam which doesn't matter // because KPTeam is included in all items by default - so effectively we have no row data if (importStatus.SkippedColumns.Count < mappingData.Mappings.Count && item.Count != 1) { listItems.Add(item); rowCount++; } else { // we have an empty row - ignore this row, but add to skippedRows if (item.Count != 1) { importStatus.SkippedRowCount++; } } } } catch (Exception ex) { string errMsg = "Error converting sheet data (ConvertSheetData). Exception: {0}; StackTrace: {1}"; importStatus.Messages.Add(string.Format(errMsg, ex.Message, ex.StackTrace)); } return(listItems); }
/// <summary> /// Ctor to create a new item - used to serialize /// a KP object into an SPItem for save and update /// </summary> /// <param name="value"></param> /// <param name="type"></param> public KPItem(string value, string type) { value = KPUtilities.ConvertToSPType(value, type); this.Value = value; this.Type = type; }