Example #1
0
        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);
        }