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>

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;

}

  1. No comments yet.
(will not be published)


%d bloggers like this: