SharePoint Custom Fields


Table of Contents

There's plenty of advice out there and I figured it made sense to put it all centrally somewhere to save others the hassle!

How To's

Rendering

Custom Properties

GetCustomProperty workaround

There are some issues with .GetCustomProperty() on a Custom Field Control.

Gotchas

'Customization' not valid within <Field>

So there's a bug with the WSS.XSD that defines the structure of the XML schema. The <Customization> element is missing, so you can't add things like this:

<Field Type="MyCustomField"
         ... omitted ...
    <Customization>
      <ArrayOfProperty>
        <Property>
          <Name>MyProperty<Name>
          <Value xmlns:q1="http://www.w3.org/2001/XMLSchema"
                 p4:type="q1:string"
                 xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">somevalue</Value>
        </Property>
      </ArrayOfProperty>
    </Customization>
  </Field>

Because SharePoint will throw this error on Deployment:

Feature definition with Id .... failed validation, file '...\elements.xml', line 100, character 6: The element 'Field' in namespace 'http://schemas.microsoft.com/sharepoint/'
has invalid child element 'Customization' in namespace 'http://schemas.microsoft.com/sharepoint/'. List of possible ele
ments expected: 'DefaultFormula, Default, DisplayPattern, FieldRefs, Formula, CHOICES, MAPPINGS, DisplayBidiPattern' in
namespace 'http://schemas.microsoft.com/sharepoint/'.

Source: MSDN

So the way to create a Custom Field with Customization's is to programmatically do it in a Feature Receiver. You can use SPSource to grab this SPField XML:

string xml = string.Format("<Field Type=\"WRC\" DisplayName=\"Test Field\" Required=\"FALSE\" Group=\"Readify Columns\" ID=\"{0}\" Name=\"TestField\"><Customization><ArrayOfProperty><Property><Name>Weight</Name><Value xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q1:double\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">3</Value></Property><Property><Name>Section</Name><Value xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q2:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">Section A</Value></Property></ArrayOfProperty></Customization></Field>", Guid.NewGuid().ToString());
string fieldId = web.Fields.AddFieldAsXml(xml);
SPField field = web.Fields[fieldDisplayName];
return field;

Provisioning Fields using XML and the Object Model

You can provision/add custom fields to a SharePoint site using XML and the Object Model. One of the gotchas here is that you must ensure that the attributes that have a boolean value (i.e. True or False) are provisioned in upper case in the XML (TRUE/FALSE). Failure to do this will mean the attribute is ignored. A good example of this is the 'Required' property, see the code below.

 using (SPSite site = new SPSite("http://yourserver"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string schemaXML = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                                       "<Field Name=\"MyCustomField\" Type=\"DateTime\" Required=\"TRUE\" ReadOnly=\"FALSE\" DisplayName=\"My Custom Field\"" StaticName=\"My Custom Field\" ShowInNewForm=\"TRUE\" ShowInDisplayForm=\"TRUE\" ShowInEditForm=\"TRUE\" ShowInListSettings=\"TRUE\" ShowInVersionHistory=\"TRUE\" ShowInViewForms=\"TRUE\" />";
                    
                    web.Fields.AddFieldAsXml(schemaXML);
                    web.Update();
                }
            }

Samples

Labels

fieldtypes fieldtypes Delete
custom custom Delete
fields fields Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.



Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.