SharePoint:FormField in a custom webpart

controltemplatedataformwebpartsharepointsharepoint-2010

On my home page, I want a simple webpart that allow users to quickly fill an entry in a list.

The list have, let's say, three fields : title (text), body (rich text), category (lookup).

I don't want to use the standard DataFormWebPart because I have a bit of code-behind that also fill some technical hidden fields of my list (actually, I don't exclude the DataFormWebPart, but I didn't find how to use it with code behind).

So I started to implement a custom webpart. Because I don't want to have to handle manually each field input, I started to use the FormField control, which automatically choose the rendering control, and provide a Value property with the correct format :

<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" />

This code is not sufficient, I have to specified the listid :

<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" ListId="{title list guid}" />

This is working quite correctly. I can in code access the fldTitle.Value to retrieve the user input.

BUT I have to include the webpart in a properly packaged and deployed feature, that can be activated. The webpart will always target the same list, but as the list is also instanciated in the feature (ListInstance element), I can't know the Guid in advance.

I've tried using several technics to set the list ID on the fly, but without success.

I've also "reflectored" the SP dlls to notice FormComponent class are using a "Context" that is set by ListFormWebPart.

Finally, my questions are :

  • is it the correct way to create a custom input webpart on the home page (not a list custom form) ?
  • how can I keep the behavior of the FormField (choose the right control and handle the input and its conversion to the storage format) ?
  • Will I have to create a custom ListFormWebPart ?
  • May I play with ControlTemplates ?

thanks in advance for the help… I'm struggling with this simple case for days now…

Best Solution

I think that customizing form templates is the easiest way to customize list forms. Since custom form templates are implemented as user controls you can add whatever code you want. See the following article: http://www.codeproject.com/KB/sharepoint/SharePointListForms.aspx

Related Question