我已经能够以编程方式将 外部 (即BDC)查找字段添加到列表中,并且我还能够添加依赖 外部 查找同一列表中的字段。我无法弄清楚的是如何以编程方式将依赖的外部查找字段添加到列表的默认视图中。
MSDN上的This文章提供了一个如何向SPView添加常规依赖查找字段的示例 - 但我还没有找到一个示例,说明如何以编程方式添加依赖 外部 查看SPView的字段。
以下是我的EventReceiver的FeatureActivated方法中的代码,我用它将依赖的外部查找字段添加到我的SharePoint列表中,并尝试将该字段添加到列表的默认视图中。
var web = ((SPSite)properties.Feature.Parent).RootWeb;
var list = web.Lists.TryGetList("MyList");
var fldName = "EmployeeID";
var fld = list.Fields.CreateNewField("BusinessData", fldName) as SPBusinessDataField;
fld.SystemInstanceName = lobSystemInstanceName;
fld.EntityNamespace = entityNamespace;
fld.EntityName = entityName;
fld.BdcFieldName = entityFieldName;
//The dictionary object defined below contains key/value pairs that represent the
//field name as a string along with a boolean flag that specifies whether or not
//the secondary field should be added to the default view.
var secondaryFieldNames = new Dictionary<string, bool>()
{
{"FirstName", true},
{"LastName", true},
{"Title", false}
}
fld.SetSecondaryFieldsNames(secondaryFieldNames.Select(e => e.Key).ToArray());
var view = list.Views.DefaultView;
foreach (var secFld in secondaryFieldNames)
{
var viewFieldName = String.Format("{0}: {1}", fldName, secFld.Key);
if (!view.ViewFields.Exists(viewFieldName) && secFld.Value)
{
view.ViewFields.Add(viewFieldName);
view.Update();
}
}
如前所述,主查找字段和所有辅助查找字段已成功添加到列表中。主查找字段已成功添加到列表的默认视图中。第二个字段不是。