Sharepoint 选择字段 [英] Sharepoint choice field

查看:43
本文介绍了Sharepoint 选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a choice field (in a site column) to reference a list I am importing into sharepoint. This list will very infrequently be updated to add additional choices. How would I create this column? Programmatically?

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

Well see it is a lookup per say... just trying to figure out how to code it... I'm assuming I need to import the list first as a new content type. Then create a lookup column (with multiple) for the content type?!?

推荐答案

以下是一些代码,用于向现有内容类型添加查找字段.

Here's some code that will add a lookup field to an exsisting content type.

如果您使用列表定义,那么这是包含查找字段的唯一方法.不能将其添加到列表定义的 CAML 中,因为需要查找列表的 guid,而这是事先未知的.创建列表时,SharePoint 会自动生成此 Guid.

If your using list definitions then this is the only way a lookup field can be included. It can't be added into the CAML of the list definition because a guid for the lookup list is required and this is not known before hand. SharePoint autogenerates this Guid when the list is created.

所以你需要先在SPSite的根SPWeb里面创建lookup列

So you need to first create the lookup column inside the root SPWeb of the SPSite

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

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

And then you'll need to add this field to the exsisting content type

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

这篇关于Sharepoint 选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆