To develop an edit/new/display form for a SharePoint list we have to implement the following steps:
- Get the list.
- Get the collection of all the columns
- If the form is in EDIT mode, get the item which is being edited.
- Render the columns that you want according to your requirements.
- Add the Save, Cancel buttons to the form and add the handler to it.
The first 3 steps are pretty straightforward. All of this information is available in the query string and SPContext.
The fourth step is interesting, because you have render the right UI component for each column. There are a couple of approaches to doing this.
Depending on the type of each column, you create the control that would go with the column. For example: if the column is a PeoplePicker, then you render a UserField control. If the column is of the type “URL” then you would render a UrlField control and so on…
Here you would write a lot of code to determine what type of control is needed for each column and to render it. A better approach is to use the FormField class.
This class works like a charm. All you have to do is set a few properties on the class and it renders the right UI for you.
The following properties need to be set on FormField class for it to render the control correctly.
- ItemId ( If in edit mode)
- ItemContext – This is the current SPContext
- FieldName – Set the name of the field to render.
- ControlMode – SPControlMode enumeration.
Once these properties are set, add the formField object to your controls collection of your container.
This will render the right UI component for you. If you need any custom UI elements for your fields, just dont render those fields using the FormField class and add them directly like you would for any other ASPX page.
Part 2 explains how to access the values and handle attachments.