|
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
RenderingCustom PropertiesGetCustomProperty workaroundThere 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:
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 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 ModelYou 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
|






