use*_*734 1 sharepoint sharepoint-2007
我需要一个选择字段(在站点列中)来引用我导入到sharepoint中的列表.此列表很少会更新以添加其他选项.我该如何创建此列?编程?
好吧,看看它是一个查找每个说...只是想弄清楚如何编码...我假设我需要首先导入列表作为一种新的内容类型.然后为内容类型创建一个查找列(有多个)?
以下是一些代码,它们将查找字段添加到现有内容类型中.
如果您使用列表定义,那么这是可以包含查找字段的唯一方法.它不能添加到列表定义的CAML中,因为需要查找列表的guid,这在手头之前是未知的.创建列表时,SharePoint会自动生成此Guid.
因此,您需要首先在SPSite的根SPWeb内创建查阅列
private void CreateLookup(SPWeb web, SPList lookupList, String lookupField, String fieldName, String fieldGroup, bool allowMultiLookup)
{
using (SPSite site = web.Site)
{
using (SPWeb rootWeb = site.RootWeb)
{
rootWeb.Fields.AddLookup(fieldName, lookupList.ID, web.ID, false);
SPFieldLookup fieldLookup = (SPFieldLookup)rootWeb.Fields[fieldName];
if (fieldLookup == null) return;
fieldLookup.AllowMultipleValues = allowMultiLookup;
fieldLookup.LookupField = lookupField;
fieldLookup.Group = fieldGroup;
fieldLookup.Title = fieldName;
fieldLookup.Update(true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要将此字段添加到现有内容类型
private void AddLookupToContentType(SPWeb web, String fieldName, String contentTypeName)
{
using (SPSite site = web.Site)
{
using (SPWeb rootWeb = site.RootWeb)
{
SPFieldLookup lookupField = (SPFieldLookup)rootWeb.Fields[fieldName];
if (lookupField == null) return;
SPContentType riskContentType = rootWeb.ContentTypes[contentTypeName];
if (riskContentType == null) return;
riskContentType.FieldLinks.Add(new SPFieldLink(lookupField));
riskContentType.Update(true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2957 次 |
最近记录: |