/// <summary> /// Takes a list of items (value/object pairs) and creates a text set of custom fields /// </summary> /// <param name="items">Dictionary of stringizable objects to be made into text custom fields</param> /// <returns>Array of TextCustomField objects representing the input dictionary</returns> public static TextCustomField[] CreateCustomFieldsArray(Dictionary <string, object> items) { // Get the number of array items to create. Each custom field can only be 100 chars long int fieldCount = 0; foreach (KeyValuePair <string, object> item in items) { fieldCount += item.Value.ToString().Length / MaxFieldLength; if (item.Value.ToString().Length % MaxFieldLength != 0) { fieldCount++; } } // Create the output array and chop up each input into custom fields var outputFields = new TextCustomField[fieldCount]; int fieldIdx = 0; foreach (KeyValuePair <string, object> item in items) { int itemFieldCount = 0; if (item.Value.ToString().Length < MaxFieldLength) { outputFields[fieldIdx] = new TextCustomField(); outputFields[fieldIdx].name = item.Key; outputFields[fieldIdx].value = item.Value.ToString(); outputFields[fieldIdx++].show = "False"; } else // fields longer than 100 characters needs to be split { for (int startCharIdx = 0; startCharIdx < item.Value.ToString().Length; startCharIdx += MaxFieldLength) { var nextCustomField = new TextCustomField(); nextCustomField.name = String.Format("{0}#{1}", item.Key, itemFieldCount++); nextCustomField.show = "False"; int stringLen = item.Value.ToString().Length; nextCustomField.value = item.Value.ToString().Substring(startCharIdx, stringLen - startCharIdx < MaxFieldLength ? stringLen - startCharIdx : 100); outputFields[fieldIdx++] = nextCustomField; } } } return(outputFields); }
/// <summary> /// Takes a list of items (value/object pairs) and creates a text set of custom fields /// </summary> /// <param name="items">Dictionary of stringizable objects to be made into text custom fields</param> /// <returns>Array of TextCustomField objects representing the input dictionary</returns> public static TextCustomField[] CreateCustomFieldsArray(Dictionary<string, object> items) { // Get the number of array items to create. Each custom field can only be 100 chars long int fieldCount = 0; foreach (KeyValuePair<string, object> item in items) { fieldCount += item.Value.ToString().Length / MaxFieldLength; if (item.Value.ToString().Length % MaxFieldLength != 0) { fieldCount++; } } // Create the output array and chop up each input into custom fields var outputFields = new TextCustomField[fieldCount]; int fieldIdx = 0; foreach (KeyValuePair<string, object> item in items) { int itemFieldCount = 0; if (item.Value.ToString().Length < MaxFieldLength) { outputFields[fieldIdx] = new TextCustomField(); outputFields[fieldIdx].name = item.Key; outputFields[fieldIdx].value = item.Value.ToString(); outputFields[fieldIdx++].show = "False"; } else // fields longer than 100 characters needs to be split { for (int startCharIdx = 0; startCharIdx < item.Value.ToString().Length; startCharIdx += MaxFieldLength) { var nextCustomField = new TextCustomField(); nextCustomField.name = String.Format("{0}#{1}", item.Key, itemFieldCount++); nextCustomField.show = "False"; int stringLen = item.Value.ToString().Length; nextCustomField.value = item.Value.ToString().Substring(startCharIdx, stringLen - startCharIdx < MaxFieldLength ? stringLen - startCharIdx : 100); outputFields[fieldIdx++] = nextCustomField; } } } return outputFields; }