Sunday, November 15, 2009

Experimenting with the ListFieldIterator webcontrol

In some circumstances you’ll need to display a SPListItem in its display, edit or new mode in an application page for example. In that case you can iterate through each SPField of an SPList, find the appropriate webcontrol of the field’s datatype and set the appropriate rendering mode of the webcontrol.


The quickest / easiest way to do so is by using the BaseFieldControl class.This way it doesn’t matter which field you want to render. The right control will be used.

BaseFieldControl webControl = [SPField].FieldRenderingControl;
webControl.ListId = list.ID;
webControl.ItemId = item.ID;
webControl.FieldName = [SPField].Title;
webControl.ID = GetControlID([SPField]);
webControl.ControlMode = mode;

Another way to display a SPListItem in your custom webpart or application page is by using the ListFieldIterator Webcontrol. This webcontrol renders each SPField of a SPListItem with an appropriate webcontrol. That way you get the SharePoint look & feel and the validation of your form for free.


ListFieldIterator_ListItem


If you’d like to use the ListFieldIterator and would like to give the user the ability to display, edit or create a new SPListItem without having permissions on the SPList itself … you must set the SetContextWeb property of the ListFieldIterator with your elevated SPWeb object.


ListFieldIterator_RunWithElevated

References:

No comments: