IT tutorials
 
Technology
 

Sharepoint 2010 : Data Access Overview - Columns (part 2) - Field Types - Inheriting from SPField

10/27/2013 9:23:45 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

1. Field Types

As you’ve seen, the SPFieldType enumeration can be used to determine the type of data that a column can contain. However, it’s possible to create custom field types that inherit from these base types and in turn use those custom field types to create columns.

There’s more to field types than simply specifying the type of data that a field can contain. Let’s take a look at the objects involved before we delve into how they interact to provide data access services for SharePoint. The following diagram shows the key objects involved in creating custom field types.

Inheriting from SPField

The SPField class is the base class for all field types. Some examples of these field types include SPFieldFile, which contains file data, and SPFieldLookup, which contains details of a lookup to a column in another list or library. All field types must ultimately derive from SPField either directly or indirectly. From a data access perspective, the SPField class is the lowest level at which we can implement custom data access code, since behind the scenes of the SPField class, the SharePoint platform handles the appropriate database interactions to persist the value of the object.

As of this writing, Visual Studio 2010 does not provide a template for creating custom field controls, so let’s take a look at how this can be done manually.

  1. Create a new Empty SharePoint project using Visual Studio 2010, as shown next.

  2. In the SharePoint Customization Wizard, be sure to select the Deploy As A Farm Solution option.

  3. Add a new class file to project named SPFieldAddress.cs. This class will be our implementation of SPField. Add the following code:

    using Microsoft.SharePoint;

    namespace CustomField
    {
    public class SPFieldAddress : SPFieldMultiColumn
    {
    public SPFieldAddress(SPFieldCollection fields, string fieldName)
    : base(fields, fieldName) { }
    public SPFieldAddress(SPFieldCollection fields, string typeName,
    string displayName): base(fields, typeName, displayName) { }

    public override object GetFieldValue(string value)
    {
    if (!string.IsNullOrEmpty(value))
    {
    return new SPAddressValue(value);
    }
    else
    {
    return null;
    }
    }

    public override string GetFieldValueAsHtml(object value)
    {
    if (value!=null)
    {
    return base.GetFieldValueAsHtml(value);
    }
    else
    {
    return "<strong>--No Address Present--</strong>";
    }
    }
    }
    }


    Our sample field will store multiple values in a single field. Rather than write a lot of the code to implement this functionality from scratch, we’ve based our field on SPFieldMultiColumn as opposed to SPField. SPFieldMultiColumn stores values of type SPFieldMultiColumnValue, and by inheriting from this class, we can create a custom data structure for our field.

  4. Add a new class named SPAddressValue.cs. Add the following code:

    using Microsoft.SharePoint;

    namespace CustomField
    {
    public class SPAddressValue : SPFieldMultiColumnValue
    {
    public SPAddressValue() : base(5) { }
    public SPAddressValue(string value) : base(value) { }

    public string StreetAddress
    {
    get { return this[0];}
    set { this[0] = value;}
    }

    public string ApartmentNumber
    {
    get { return this[1];}
    set { this[1] = value;}
    }

    public string City
    {
    get { return this[2];}
    set { this[2] = value;}
    }

    public string State
    {
    get { return this[3];}
    set { this[3] = value;}
    }

    public string Zip
    {
    get { return this[4];}
    set { this[4] = value;}
    }
    }
    }


  5. So that the SharePoint platform knows about our new field, we need to create a file containing some definition XML. The file should be deployed to %SPROOT%Template\xml. Choose Project | Add SharePoint Mapped Folder.

  6. Map Template\Xml and then create a new XML file in the mapped folder named fldtypes_FieldDemo.xml. When creating field definition files, it is important that they be named appropriately. SharePoint loads files with names in the format fldtypes_<whatever>.xml

  7. Add the following XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <FieldTypes>
    <FieldType>
    <Field Name="TypeName">AddressField</Field>
    <Field Name="ParentType">MultiColumn</Field>
    <Field Name="TypeDisplayName">Demo Address Field</Field>
    <Field Name="TypeShortDescription">
    Demonstration custom field for entering addresses</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="FieldTypeClass">
    CustomField.SPFieldAddress, $SharePoint.Project.AssemblyFullName$</Field>
    <RenderPattern Name="HeaderPattern">
    <Property Select="DisplayName" HTMLEncode="TRUE"/>
    </RenderPattern>
    <RenderPattern Name="DisplayPattern">
    <Switch>
    <Expr>
    <Column/>
    </Expr>
    <Case Value="">
    <HTML>
    <![CDATA[--No Address Present--]]>
    </HTML>
    </Case>
    <Default>
    <Column SubColumnNumber="0" HTMLEncode="TRUE"/>
    <HTML><![CDATA[,]]></HTML>
    <Column SubColumnNumber="1" HTMLEncode="TRUE"/>
    <HTML><![CDATA[,]]></HTML>
    <Column SubColumnNumber="2" HTMLEncode="TRUE"/>
    <HTML><![CDATA[,]]></HTML>
    <Column SubColumnNumber="3" HTMLEncode="TRUE"/>
    <HTML><![CDATA[,]]></HTML>
    <Column SubColumnNumber="4" HTMLEncode="TRUE"/>
    </Default>
    </Switch>
    </RenderPattern>
    </FieldType>
    </FieldTypes>
 
Others
 
- Sharepoint 2010 : Data Access Overview - Columns (part 1)
- Sharepoint 2010 : Data Access Overview - Content Types (part 3) - Enterprise Content Types
- Sharepoint 2010 : Data Access Overview - Content Types (part 2) - Content Type Metadata
- Sharepoint 2010 : Data Access Overview - Content Types (part 1) - Content Type Inheritance
- Windows 7 : Using CDs and DVDs - Using Disks that Already Contain Data (part 2) - Changing what happens when you insert a CD or DVD
- Windows 7 : Using CDs and DVDs - Using Disks that Already Contain Data (part 1)
- Windows 7 : Using CDs and DVDs - What Kind of Drive Do I Have?
- Windows 7 : Kernel Mode Installation and Build - Testing a KMDF Driver
- Windows 7 : Kernel Mode Installation and Build - Catalog Files and Digital Signature, Installing Featured Toaster
- Windows Phone 8 : Enterprise Phone Apps - Preparing Apps for Distribution, Building a Company Hub
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us