Seiten

Dienstag, 18. Oktober 2011

Programmatically change Sharepoint list forms

A very common request for a sharepoint developer is to customize the form of a list. With Sharpeoint Designer this is really easy to do but this has the downside that a) designer forms with Sharepoint Designer is harder than it looks and b) it comes with all the downsides of customization vs definition such as not having a deployable solution. Another option is to use InfoPath Designer but this is not cheap because you need Office Pro and the enterprise version of Sharepoint.
The same in object model, please

the same as SPList.Forms
Another, very attractive option is to use ASP.NET markup and code to define the design and functionality of a form.

Enumeration of Sharepoint list forms can be done via SPList.Forms. Using this you can find out which forms are registered for the list, which type they have (edit, new, display) and which one is the default form is several of one type exist.   

Since SPList.Forms is a read only collection you have to use another way of changing the forms. When a list is created, each form is provisioned as a web part page in the list itself. These pages are either stored in the hidden Forms folder of document libraries or in the RootFolder of standard lists and can be accessed as normal SPFile objects.
(http://msdn.microsoft.com/en-us/library/aa544154.aspx)

Each of these web part pages contains a WebPartManager with one ListFormWebPartUsing the TemplateName property you can specify a custom rendering template and thus create a completely individual form body while maintaining all the forms standard setup, such as buttons and toolbar.

To customize the form content whilst retaining the default setup of form page you can specify a custom rendering template for the ListFormWebPart. The rendering template is an ASCX file in TEMPLATES\CONTROLTEMPLATES and contains a <sharepoint:renderingtemplate id="[Your template name]"> tag. 

If you can assign a content type to the list you have a much easier life because you can use the NewFormTemplateName and EditFormTemplateName of the content type object. However its not always possible to assign content types, e.g. for external lists. Especially for these I find it very useful to be able to deploy a custom form.


Keine Kommentare:

Kommentar veröffentlichen