Using Native SharePoint Form Fields

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.

///<summary>

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

///</summary>

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

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

///<returns></returns>

privateFieldLabel buildLabel(SPContext newItemContext, SPField fld)

{

FieldLabel lbl = newFieldLabel();

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