I'm creating a custom New Form for a Link list. Depending on the view depends which columns are displayed.
for(int i = 0; i < spView.ViewFields.Count; i++)
{
string fieldName = spView.ViewFields[i];
if (fieldName.Contains("LinkTitle"))
fieldName = "Title";
SPField field = spList.Fields.GetFieldByInternalName(fieldName);
if (field == null || field.FieldRenderingControl == null || field.Hidden)
continue;
Control cntrl = GetSharePointControls(field, spList, itemId);
if(cntrl == null) continue;
//Add control to webpart.
}
Now to create the control I use the following code.
internal 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;
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);
webControl.RenderContext = controlContext;
webControl.ItemContext = controlContext;
ctrl = webControl;
return ctrl;
}
Now if I add a URL to a generic list, then this code works out the field is a URL field and displays the control correctly on the page. However, when using a Link list, the URL field type is a calculated column, which doesn't end up rendering on the page. I also found it strange that webControl.Visible = false; and if you change it in code to true, it still remains false.
I have also tried converting directly to a ComputedField instead of BaseFieldControl, but I end up with the same result. Has anyone else encountered this, or can see a fix for me? I might have to add extra logic for link lists, so that the field name URL is converted to a URLField.