Sharepoint选择字段

时间:2009-03-24 00:07:00

标签: sharepoint sharepoint-2007

我需要一个选择字段(在站点列中)来引用我导入到sharepoint中的列表。此列表很少会更新以添加其他选项。我该如何创建此列?编程?

看到它是一个查找每个说...只是想弄清楚如何编码...我假设我需要首先导入列表作为新的内容类型。然后为内容类型创建一个查找列(有多个)?

2 个答案:

答案 0 :(得分:6)

这里有一些代码会将查询字段添加到现有内容类型中。

如果使用列表定义,那么这是查找字段可以包含的唯一方法。它不能添加到列表定义的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);
        }
    }
}

然后您需要将此字段添加到现有内容类型

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);
        }
    }
}

答案 1 :(得分:0)

听起来像Lookup专栏是您正在寻找的。您必须先导入列表,然后将查找创建为站点列。

相关问题