Using Native SharePoint Form Fields

Slalom Consultant Maarten Sundman

Slalom Consultant Maarten Sundman specializes in .NET, SharePoint, and Silverlight solutions and has experience in the Financial Services, Software, and Utilities and Energy sectors.

by Maarten Sundman

When making a generic web part for modifying or interacting with different SharePoint data you can’t always predict the column data type before hand.

The best way to make sure your web part works regardless of what data is being tossed in to it is to just use the out of box SharePoint Form Field control. This control goes ahead and grabs the column edit and display templates depending on the state you specify.

Below is a code snippet example of how to use this control. Takes a lot of the headache out when dealing with unknown data types/building generic web parts.

<pre>///<summary>

/// returns a valid native sharepoint fieldlabel control for use in forms

///</summary>

///<param name=”newItemContext”></param>

///<param name=”fld”></param>

///<returns></returns>

private FieldLabel buildLabel(SPContext newItemContext, SPField fld)

{

FieldLabel lbl = new FieldLabel();

lbl.ControlMode = SPControlMode.New;

lbl.FieldName = fld.Title;

//lbl.ItemContext = newContext;

lbl.RenderContext = newItemContext;

lbl.ID = String.Format(LABEL, fld.InternalName);

return lbl;

}

///<summary>

/// Returns a valid native sharepoint field control based ont he field type/definition of the passed spfield object for use in forms

///</summary>

///<param name=”newItemContext”></param>

///<param name=”fld”></param>

///<param name=”listID”></param>

///<returns></returns>

private FormField buildFormField(SPContext newItemContext, SPField fld, Guid listID)

{

//build form field interaction UI using native formfield control

int id = mainFormFields.Count + repeatingFormFields.Count;

FormField formField = new FormField();

formField.ControlMode = SPControlMode.New;

formField.ListId = listID;

formField.FieldName = fld.InternalName;

formField.RenderContext = newItemContext;

formField.ID = String.Format(FIELD, fld.Id.ToString());

return formField;

}

– Maarten

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: