使用FieldRendingControl获取给定的内容类型
-
09-12-2019 - |
题
我正在创建一个像新表单一样的webpart。基于一个视图,我正在使用spfield.fieldRenderingControl在我的WebPart中获取字段。
我的列是站点列,在此级别中已经需要一些,添加到内容类型,该类型已添加到列表中。在添加到列表中后,已经需要某些不需要的列,并且现在的站点列级别需要一些列。
下面是我正在使用的代码呈现我的表单。无论我尝试了什么,控制控件的渲染似乎从网站列级别抓取,而不是列表内容类型级别。我无法从spfieldlink渲染控件。
我在我认为可能需要修改的区域周围的代码中提出评论。
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;
}
}
.
如果有人有任何想法,我会非常感激。我甚至甚至试图反思Microsoft代码,成功有限。
解决方案 2
我介绍了这一点,看起来SPFIELD在列表字段的所需状态下读取。但是在自定义列表中,您可以通过单击列表设置中的列名称来编辑所需字段。
对于文档库,缺少必填字段设置。您可以设置所需字段的唯一方法是在网站内容类型中,然后按下更改,(将在您使用它的任何地方更改所需的字段,而不仅仅在给定列表中)或您的代码,使用.NET或PowerShell将列表中的字段设置为必需。然后将原始代码中的任何引用删除到spfieldlink。
$web = Get-SPWeb "[YourSite]"
$list = $web.GetListFromUrl("[Your List URL]")
$field = $list.Fields["[Field Name"]
Write-Host "Previous Value:" $field.Required
$field.Required = $true
$field.Update()
Write-Host "The field" $field.Title "is now set to" $field.Required
.
奇怪的是,与文档库相比,原始代码在自定义列表中发布的原始代码非常不同。
我发现了这一点,通过在我的webpart中显示字段描述。如果本地描述与站点列描述不同,则当您调用Field.description时,您将始终收到本地描述。这意味着从列表中调用SPField确实为您提供了从站点列中列表中给出的列的值。因此,如果需要列表中的列,我会向查询发出一个查询,虽然它位于网站列中,但它不是列表。
替代修复而不是使用powershell是创建一个布局页面,您从磁带库设置调用,允许您修改列表中每个列的所需值,如Microsoft错过了此功能。 (也许是一个我没有找到的好理由)。
其他提示
也许可以查看如何如何编辑项目Batchedit它: http://sp2010batchedit.codeplex.com/