I'm creating a webpart that acts like a new form. Based on a view I'm getting on fields and rendering them in my webpart using spfield.FieldRenderingControl.
My columns are site columns, at this level some have been made required, added to a content type, which has been added to a list. After added to the list, certain columns that were not required have been made required, and some that were required at the site column level are now not.
Below is the code I'm using to render my form. No matter what I have tried, the rendering of the control seems to be grabbed from the site column level, not the list content type level. I'm unable to Render the control from a SPFieldLink.
I've put comments within the code around the area I think might need amending.
try
{
Table table = new Table();
table.CssClass = "hlwp_DSCreateEditTable";
TableRow row;
TableCell cell;
SPContentType ct = spList.ContentTypes[ContentTypeName];
for (int i = 0; i < spView.ViewFields.Count; i++)
{
string fieldName = spView.ViewFields[i];
//The flink holds the information at the content type level in the list.
SPFieldLink flink = ct.FieldLinks[fieldName];
SPField field = spList.Fields.GetField(fieldName);
row = new TableRow();
row.CssClass = "hlwp_DSCreateEditRow";
cell = new TableCell();
cell.CssClass = "hlwp_DSCreateEditTitleCell";
//Even tried setting the field.required to match the flink.required.
//When debugging and following the code through it stays the same as flink.required,
//but if you call Page.Validate() it displays errors on the controls
//that are required at the site column level not list content type level.
if (flink != null)
{
field.Required = flink.Required;
}
if (field.Required)
{
cell.Text = field.Title + "<font color='red'>*</font>";
}
else
{
cell.Text = field.Title;
}
row.Cells.Add(cell);
cell = new TableCell();
cell.CssClass = "hlwp_DSCreateEditControlCell";
Control cntrl = HelperClass.GetSharePointControls(field, spList, itemId);
if (cntrl == null) continue;
cell.Controls.Add(cntrl);
row.Cells.Add(cell);
table.Rows.Add(row);
}
HelperClass.GetSharePointControls
public static Control GetSharePointControls(SPField field, SPList list, int itemId)
{
// check if the field is a buildIn field, or can be rendered by a SharePoint Control
if (field == null || field.FieldRenderingControl == null || field.Hidden) return null;
Control ctrl = null;
SPControlMode mode = SPControlMode.Invalid;
if (itemId > 0)
mode = SPControlMode.Edit;
else
mode = SPControlMode.New;
//Wondering if it's something to do with the context, as later on you set this context to the RenderingContext.
var controlContext = SPContext.GetContext(System.Web.HttpContext.Current, itemId, list.ID, SPContext.Current.Web);
SPContext.Current.FormContext.SetFormMode(mode, true);
controlContext.FormContext.SetFormMode(mode, true);
try
{
BaseFieldControl webControl = field.FieldRenderingControl;
webControl.ListId = list.ID;
webControl.ControlMode = mode;
webControl.ItemId = itemId;
webControl.FieldName = field.Title;
webControl.ID = GetControlID(field); //Creates a unique ID.
webControl.RenderContext = controlContext;
webControl.ItemContext = controlContext;
ctrl = webControl;
}
return ctrl;
}
catch (Exception ex)
{
var errorLabel = new Label
{
ID = "ErrorLabel",
Text = String.Format("Error in GetSharePointControls:<br/>{0}", ex)
};
return errorLabel;
}
}
If anyone has any ideas I would be very grateful. I've even tried to reflect on Microsoft code with limited success.