Beispiel #1
0
        public static string FormatValueAsText(this SPFieldCalculated calcField, string rawValue)
        {
            string      result         = rawValue;
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

            switch (calcField.OutputType)
            {
            case SPFieldType.Number:
                try
                {
                    var numberValue = Convert.ToDouble(rawValue);
                    result = SPFieldNumber.GetFieldValueAsText(numberValue, currentCulture, calcField.ShowAsPercentage, calcField.DisplayFormat);
                }
                catch { result = string.Empty; }
                break;

            case SPFieldType.DateTime:
                try
                {
                    var dateValue = Convert.ToDateTime(rawValue);
                    result = SPFieldDateTime.GetFieldValueAsText(dateValue, SPContext.Current.Web, calcField.DateFormat == SPDateTimeFieldFormatType.DateTime ? SPDateFormat.DateTime : SPDateFormat.DateOnly);
                }
                catch { result = string.Empty; }
                break;

            case SPFieldType.Currency:
                try
                {
                    double data = Convert.ToDouble(rawValue, CultureInfo.InvariantCulture);
                    result = SPFieldCurrency.GetFieldValueAsText(data, currentCulture, calcField.CurrencyLocaleId, calcField.DisplayFormat);
                }
                catch { result = string.Empty; }
                break;

            case SPFieldType.Boolean:
                try
                {
                    result = Convert.ToBoolean(Convert.ToByte(rawValue)).ToString();
                }
                catch { }
                break;

            default:
                break;
            }
            return(result);
        }
Beispiel #2
0
        private string GetFieldValue(SPField field, SPListItemVersion version)
        {
            string      fieldValue = string.Empty;
            SPFieldType fieldType  = field.Type;

            switch (fieldType)
            {
            case SPFieldType.Lookup:
                SPFieldLookup newField = (SPFieldLookup)field;
                fieldValue = newField.GetFieldValueAsText(version[field.StaticName]);
                break;

            case SPFieldType.User:
                SPFieldUser newUser = (SPFieldUser)field;
                fieldValue = newUser.GetFieldValueAsText(version[field.StaticName]);
                break;

            case SPFieldType.ModStat:
                SPFieldModStat modStat = (SPFieldModStat)field;
                fieldValue = modStat.GetFieldValueAsText(version[field.StaticName]);
                break;

            case SPFieldType.URL:
                SPFieldUrl urlField = (SPFieldUrl)field;
                fieldValue = urlField.GetFieldValueAsHtml(version[field.StaticName]);
                break;

            case SPFieldType.DateTime:
                SPFieldDateTime newDateField = (SPFieldDateTime)field;
                if (!string.IsNullOrEmpty(newDateField.GetFieldValueAsText(version[field.StaticName])))
                {
                    if (newDateField.DisplayFormat == SPDateTimeFieldFormatType.DateTime)
                    {
                        fieldValue = DateTime.Parse(newDateField.GetFieldValueAsText(version[field.StaticName])).ToString();
                    }
                    else
                    {
                        fieldValue = DateTime.Parse(newDateField.GetFieldValueAsText(version[field.StaticName])).ToShortDateString();
                    }
                }
                break;

            case SPFieldType.Invalid:

                // http://sharepointnadeem.blogspot.com/2013/09/sharepoint-spfieldtype-is-invalid-for.html
                if (field.TypeAsString.Equals("TaxonomyFieldType") || field.TypeAsString.Equals("TaxonomyFieldTypeMulti"))
                {
                    TaxonomyField taxonomyField = field as TaxonomyField;
                    fieldValue = taxonomyField.GetFieldValueAsText(version[field.StaticName]);
                }
                else
                {
                    fieldValue = version[field.StaticName].ToString();
                }
                break;

            default:
                fieldValue = version[field.StaticName].ToString();
                break;
            }

            return(fieldValue);
        }