public static string GetFormulaValue(this SPListItem item, string formula) { string regex = @"\[[\w*\s0-9]*\]"; var matches = Regex.Matches(formula, regex); string result = formula; if (matches != null) { foreach (Match match in matches) { if (string.IsNullOrEmpty(match.Value)) { continue; } var temp = match.Value.Remove(0, 1); string fieldName = temp.Remove(temp.Length - 1, 1); string fieldValue = string.Empty; if (item.Fields.ContainFieldName(fieldName) || item.Fields.ContainsFieldWithStaticName(fieldName)) { fieldValue = item.GetStringValue(fieldName); } result = result.Replace(match.Value, fieldValue); } } return(result); }
public static object GetObjectValue(this SPListItem item, SPField field) { object result = null; switch (field.Type) { case SPFieldType.Text: { result = item.GetStringValue(field); break; } case SPFieldType.Number: case SPFieldType.Currency: { result = item.GetNumberValue(field); break; } case SPFieldType.DateTime: { result = item.GetDateTimeValue(field); break; } case SPFieldType.MultiChoice: { var multiChoice = item.GetMultipleChoiceValueCollection(field); if (multiChoice != null) { result = multiChoice.ToString(); } break; } case SPFieldType.Choice: { result = item.GetChoiceValue(field); break; } case SPFieldType.Lookup: { var lookupField = (field as SPFieldLookup); if (lookupField != null) { if (lookupField.AllowMultipleValues) { var lookupValue = item.GetLookupValueCollection(field); if (lookupValue != null) { result = lookupField.IsRelationship ? String.Join(",", lookupValue.Select(value => value.LookupId)) : String.Join(",", lookupValue.Select(value => value.LookupValue)); } } else { var lookupValue = item.GetLookupValue(field); if (lookupValue != null) { if (lookupField.IsRelationship) { result = lookupValue.LookupId; } else { result = lookupValue.LookupValue; } } } } break; } case SPFieldType.Boolean: { result = item.GetTrueFalseValue(field); break; } case SPFieldType.User: { var userValue = item.GetUserValue(field); if (userValue != null) { result = (userValue.User == null) ? userValue.LookupValue : userValue.User.LoginName; } break; } case SPFieldType.URL: { var url = item.GetUrlValue(field); if (url != null) { result = url.Url; } break; } default: { var defaultValue = item[field.Id].ToString(); if (!string.IsNullOrWhiteSpace(defaultValue)) { result = defaultValue.Contains(";#") ? String.Join(",", new SPFieldLookupValueCollection(defaultValue).Select(value => value.LookupId)) : defaultValue; } break; } } return(result); }