Rene
Rene

Reputation: 21

SharePoint ListfieldIterator default values

I'm building a custom web part in SharePoint, that renders a form to create new listitems. I have a listfielditerator with the ListId set and ControlMode.New, and a savebutton. This works!

The only additional functionality is to put dynamic default values on the rendered field controls. I thought i could achieve this as follows:

In OnInit, to make the iterator work:

SPContext.Current.FormContext.FormMode = SPControlMode.New;

newFormIterator.ListId = SPContext.Current.Web.Lists["MyList"].ID;
newFormIterator.ItemContext = SPContext.GetContext(HttpContext.Current,0, newFormIterator.ListId, SPContext.Current.Web);

In OnInit or OnLoad or OnPrerender, to set values:

TextField field = (TextField)newFormIterator.Fields["Customer"].FieldRenderingControl;
//field.ItemContext = newFormIterator.ItemContext;
//field.RenderContext = newFormIterator.ItemContext;
//field.ListItemFieldValue = settings.CustomerName;
field.Value = settings.CustomerName;
field.UpdateFieldValueInItem();

I have tried the commented lines also, but without result. The problem with this is an exception, because field.Item is null when UpdateFieldValueInItem() is called.

Questions:

  1. How can I use the listfielditerator properly outside the listcontext (in a web part on an ordinary page). Am i missing context?
  2. How can I set default values on fields in a listfielditerator, other than specified on the content type fields of the list.

Read resources:

http://karinebosch.wordpress.com/sharepoint-controls/listfielditerator/

http://pholpar.wordpress.com/2009/12/

Upvotes: 1

Views: 2099

Answers (1)

Ivan Vagunin
Ivan Vagunin

Reputation: 361

I guess you should not call field.UpdateFieldValueInItem(); directly - setting value for fielf rendering control in OnInit should be enough. UpatedFieldValueInItem() will be called automatically when SaveButton is pressed and item is created. So try just this:

TextField field = (TextField)newFormIterator.Fields["Customer"].FieldRenderingControl;
field.Value = settings.CustomerName;

Upvotes: 0

Related Questions