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;

See this article for a longer explanation / more code!

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.