private static bool GetAdditionalPropertiesDefinitionsFromCsv( ref string sChosenFile, ref List <AdditionalPropertyDefinition> oAdditionalPropertyDefinitions) { bool bRet = false; string sFileContents = string.Empty; bRet = GetCsvFileContents(ref sChosenFile, ref sFileContents); if (bRet == false) { return(false); } // return GetAdditionalPropertiesDefinitionsFromString(sFileContents, ref oAdditionalPropertyDefinitions); // Look at replacing whats below with the line above. // The file is now loaded.... oAdditionalPropertyDefinitions = new List <AdditionalPropertyDefinition>(); //bRet = GetCsvFile(sFile, ref sFileContents); List <string> listHeaders = new List <string>(); sFileContents = sFileContents.Replace("\r\n", "\n"); sFileContents = sFileContents.Replace("\r", "\n"); sFileContents = sFileContents.Replace("\n", "\r\n"); string[] sArrSplitLines = { "\r\n" }; string[] sLines = sFileContents.Split(sArrSplitLines, StringSplitOptions.None); string[] sArrSplitFields = { "," }; string[] sColumns; int iColumnCount = 0; int iPropertyId = 0; AdditionalPropertyDefinition oAdditionalPropertyDefinition = null; string sUseLine = string.Empty; bool bFoundFirstDataLine = false; string sPropertySetId = string.Empty; //Console.WriteLine("============ GetAdditionalPropertiesDefinitionsFromCsv ====" + DateTime.Now.ToString() + " =================="); int iLine = 0; foreach (string sLine in sLines) { iLine++; sUseLine = sLine.Replace("\"", ""); sUseLine = sUseLine.Trim(); if (sUseLine.Length == 0 || (sUseLine.Trim().StartsWith("//") == true)) // skip blank lines and comment lines. { continue; } if (bFoundFirstDataLine == false) { // This should be the header line. If so then skip it. bFoundFirstDataLine = true; } else { // These should all be lines to process. oAdditionalPropertyDefinition = new AdditionalPropertyDefinition(); sColumns = sUseLine.Split(sArrSplitFields, StringSplitOptions.None); iColumnCount = sColumns.Count <string>(); if (iColumnCount != 5) { string sInfo = (string.Format("Line {0} of the CSV file does not have the correct number of columns. See line with the following text:\r\n{1}", iLine, sUseLine)); MessageBox.Show(sInfo, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } bool bIsNumber = false; // Property ID can be different things string sVal = sColumns[3].Trim().ToUpper(); try {// PropertyIdIsString oAdditionalPropertyDefinition.PropertySetIdString = ""; oAdditionalPropertyDefinition.PropertyId = 0; oAdditionalPropertyDefinition.PropertyIdIsString = false; if (StringHelper.IsInteger(sVal)) { bIsNumber = true; } if (sVal.StartsWith("0X")) { bIsNumber = true; } if (bIsNumber == false) { // This would be a named property - so, no id. oAdditionalPropertyDefinition.PropertyIdIsString = true; oAdditionalPropertyDefinition.PropertySetIdString = sVal; } if (bIsNumber == true) { oAdditionalPropertyDefinition.PropertyIdIsString = false; if (sVal.StartsWith("0X")) // Hex value? { sVal = sVal.Replace("0X", ""); // remove hex prefix iPropertyId = Convert.ToInt32(sVal, 16); // Convert from hex to int. } else { iPropertyId = Convert.ToInt32(sColumns[3].Trim()); // value is already an int. } oAdditionalPropertyDefinition.PropertyId = iPropertyId; } } catch (Exception ex) { MessageBox.Show(string.Format("Line {0} of the CSV file has a non-numeric PropertyId. See {1} ", iLine, sColumns[1].Trim()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } oAdditionalPropertyDefinition.DescPropertyName = sColumns[0].Trim(); oAdditionalPropertyDefinition.PropertyName = sColumns[1].Trim(); // oAdditionalPropertyDefinition.PropertyId = iPropertyId; oAdditionalPropertyDefinition.PropertyType = sColumns[4].Trim(); sPropertySetId = EwsExtendedPropertyHelper.TrySwapGuidForPropSetName(sColumns[2].Trim(), oAdditionalPropertyDefinition.PropertyId, oAdditionalPropertyDefinition.PropertyType, sUseLine, iLine ); oAdditionalPropertyDefinition.PropertySetId = sPropertySetId; //oAdditionalPropertyDefinition.PropertyDefinitionType = sColumns[3].Trim(); oAdditionalPropertyDefinitions.Add(oAdditionalPropertyDefinition); // For debugging - start //string sDebug = string.Format("PropertyName: {0}, PropertyType: {1}, PropSetId: {2}, PropertyId: {3}, PropertyIdIsString {4}, PropertySetIdString {5}, Line: {6}\r\n", // oAdditionalPropertyDefinition.PropertyName, // oAdditionalPropertyDefinition.PropertyType, // oAdditionalPropertyDefinition.PropertySetId, // oAdditionalPropertyDefinition.PropertyId.ToString(), // oAdditionalPropertyDefinition.PropertyIdIsString, // oAdditionalPropertyDefinition.PropertySetIdString, // iLine.ToString() // ); //Console.Write(sDebug); //if (oAdditionalPropertyDefinition.PropertyId == 0 && oAdditionalPropertyDefinition.PropertyIdIsString == false) // Console.WriteLine("***** (oAdditionalPropertyDefinition.PropertySetId == 0 && PropertyIdIsString == false)"); // For debugging - end } } bRet = true; return(bRet); }
public static bool GetAdditionalPropertiesDefinitionsFromString( string sFileContents, ref List <AdditionalPropertyDefinition> oAdditionalPropertyDefinitions) { bool bRet = false; oAdditionalPropertyDefinitions = new List <AdditionalPropertyDefinition>(); //bRet = GetCsvFile(sFile, ref sFileContents); List <string> listHeaders = new List <string>(); sFileContents = sFileContents.Replace("\r\n", "\n"); sFileContents = sFileContents.Replace("\r", "\n"); sFileContents = sFileContents.Replace("\n", "\r\n"); string[] sArrSplitLines = { "\r\n" }; string[] sLines = sFileContents.Split(sArrSplitLines, StringSplitOptions.None); string[] sArrSplitFields = { "," }; string[] sColumns; int iColumnCount = 0; int iPropertyId = 0; AdditionalPropertyDefinition oAdditionalPropertyDefinition = null; string sUseLine = string.Empty; bool bFoundFirstDataLine = false; string sPropertySetId = string.Empty; int iLine = 0; foreach (string sLine in sLines) { iLine++; sUseLine = sLine.Replace("\"", ""); sUseLine = sUseLine.Trim(); if (sUseLine.Length == 0 || (sUseLine.Trim().StartsWith("//") == true)) // skip blank lines and comment lines. { continue; } if (bFoundFirstDataLine == false) { // This should be the header line. If so then skip it. bFoundFirstDataLine = true; } else { // These should all be lines to process. oAdditionalPropertyDefinition = new AdditionalPropertyDefinition(); sColumns = sUseLine.Split(sArrSplitFields, StringSplitOptions.None); iColumnCount = sColumns.Count <string>(); if (iColumnCount != 5) { string sInfo = (string.Format("Line {0} of the CSV file does not have the correct number of columns. See line with the following text:\r\n{1}", iLine, sUseLine)); MessageBox.Show(sInfo, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } oAdditionalPropertyDefinition.DescPropertyName = sColumns[0].Trim(); oAdditionalPropertyDefinition.PropertyName = sColumns[1].Trim(); oAdditionalPropertyDefinition.PropertyType = sColumns[4].Trim(); //oAdditionalPropertyDefinition.PropertyId = iPropertyId; oAdditionalPropertyDefinition.PropertyId = 0; oAdditionalPropertyDefinition.PropertyIdIsString = false; oAdditionalPropertyDefinition.PropertySetIdString = ""; bool bIsNumber = false; string sVal = sColumns[3].Trim().ToUpper(); if (StringHelper.IsInteger(sVal)) { bIsNumber = true; } if (sVal.StartsWith("0X")) { bIsNumber = true; } if (bIsNumber == false) { // This would be a named property - so, no id. oAdditionalPropertyDefinition.PropertyIdIsString = true; oAdditionalPropertyDefinition.PropertySetIdString = sVal; } if (bIsNumber == true) { try { if (sVal.StartsWith("0X")) // Hex value? { sVal = sVal.Replace("0X", ""); // remove hex prefix iPropertyId = Convert.ToInt32(sVal, 16); // Convert from hex to int. } else { iPropertyId = Convert.ToInt32(sColumns[3].Trim()); // value is already an int. } oAdditionalPropertyDefinition.PropertyId = iPropertyId; } catch (Exception ex) { MessageBox.Show(string.Format("Line {0} of the CSV file has a non-numeric PropertyId. See {1} ", iLine, sColumns[1].Trim()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } } if (sPropertySetId.StartsWith("http")) { System.Diagnostics.Debug.WriteLine("Test"); } sPropertySetId = EwsExtendedPropertyHelper.TrySwapGuidForPropSetName(sColumns[2].Trim(), oAdditionalPropertyDefinition.PropertyId, oAdditionalPropertyDefinition.PropertyType, sUseLine, iLine ); oAdditionalPropertyDefinition.PropertySetId = sPropertySetId; oAdditionalPropertyDefinitions.Add(oAdditionalPropertyDefinition); } } bRet = true; return(bRet); }