SharePoint: Numeric/Integer Site Column (Field) Types
- by CharlesLee
What field type should you use when creating number based site columns as part of a SharePoint feature? Windows SharePoint Services 3.0 provides you with an extensible and flexible method of developing and deploying Site Columns and Content Types (both of which are required for most SharePoint projects requiring list or library based data storage) via the feature framework (more on this in my next full article.) However there is an interesting behaviour when working with a column or field which is required to hold a number, which I thought I would blog about today. When creating Site Columns in the browser you get a nice rich UI in order to choose the properties of this field: However when you are recreating this as a feature defined in CAML (Collaborative Application Mark-up Language), which is a type of XML (more on this in my article) then you do not get such a rich experience. You would need to add something like this to the element manifest defined in your feature: <Field SourceID="http://schemas.microsoft.com/sharepoint/3.0" ID="{C272E927-3748-48db-8FC0-6C7B72A6D220}" Group="My Site Columns" Name="MyNumber" DisplayName="My Number" Type="Numeric" Commas="FALSE" Decimals="0" Required="FALSE" ReadOnly="FALSE" Sealed="FALSE" Hidden="FALSE" /> OK, its not as nice as the browser UI but I can deal with this. Hang on. Commas="FALSE" and yet for my number 1234 I get 1,234. That is not what I wanted or expected. What gives? The answer lies in the difference between a type of "Numeric" which is an implementation of the SPFieldNumber class and "Integer" which does not correspond to a given SPField class but rather represents a positive or negative integer. The numeric type does not respect the settings of Commas or NegativeFormat (which defines how to display negative numbers.) So we can set the Type to Integer and we are good to go. Yes? Sadly no! You will notice at this point that if you deploy your site column into SharePoint something has gone wrong. Your site column is not listed in the Site Column Gallery. The deployment must have failed then? But no, a quick look at the site columns via the API reveals that the column is there. What new evil is this? Unfortunately the base type for integer fields has this lovely attribute set on it: UserCreatable = FALSE So WSS 3.0 accordingly hides your field in the gallery as you cannot create fields of this type. However! You can use them in content types just like any other field (except not in the browser UI), and if you add them to the content type as part of your feature then they will show up in the UI as a field on that content type. Most of the time you are not going to be too concerned that your site columns are not listed in the gallery as you will know that they are there and that they are still useable. So not as bad as you thought after all. Just a little quirky. But that is SharePoint for you.