SharePoint: FormField Control

Just discovered this control the other day. It renders SharePoint field types (and custom field types) as they would appear on a SharePoint form. All you need to pass to it it the id of a list and field name. It can also be used generate a form element in edit mode (i.e. with values populated).

using (var site = new SPSite(“http://mywebsite”))
{
    using (var web = site.OpenWeb())
    {
        var list = web.Lists[this.ListName];

        foreach (SPField field in list.Fields)
        {
            if (field.FieldRenderingControl != null &&
              !field.Hidden && !field.ReadOnlyField &&
              field.Type != SPFieldType.Attachments)
            {
                var currentField = new FormField();
                currentField.ListId = list.ID;
                currentField.FieldName = field.InternalName;
                currentField.ID = “Control_” + field.InternalName;
                currentField.ControlMode = SPControlMode.New;
                this.Controls.Add(currentField);
            }
        }
    }
}

See this article for a longer explanation / more code!

http://www.fivenumber.com/sharepoint-list-form-generator/