简体   繁体   English

如何使用Visual Studio 2010在SharePoint 2010中的所有文档库列表中添加新列?

[英]How Can I Add a New Column to All Document Library Lists in SharePoint 2010 Using Visual Studio 2010?

I can't figure out how to create a solution in Visual Studio 2010 that will allow me to alter existing lists in a SharePoint 2010 site. 我无法弄清楚如何在Visual Studio 2010中创建解决方案,该解决方案允许我更改SharePoint 2010网站中的现有列表。 Specifically, I want to add a new column, which contains a small icon, to existing document library lists. 具体来说,我想在现有文档库列表中添加一个包含小图标的新列。 I want to be able to take an action when someone clicks on one of the new icons. 我希望有人单击新图标之一时可以采取措施。 I also want this new column to become part of the default view for new document library lists. 我还希望此新列成为新文档库列表的默认视图的一部分。 All of this needs to be easily deployed to a SharePoint 2010 site via a .wsp file. 所有这些都需要通过.wsp文件轻松部署到SharePoint 2010网站。

Extensive searching on Google has shown how to create new lists and new column types, and how to programmatically add columns to one of the new lists, but not how to modify all existing lists. 在Google上进行的广泛搜索显示了如何创建新列表和新列类型,以及如何以编程方式将列添加到新列表之一,但没有展示如何修改所有现有列表。

I'm brand new to SharePoint, and any pointers towards a solution would be much appreciated. 我是SharePoint的新手,对于解决方案的任何建议将不胜感激。 Thanks! 谢谢!

If you really want to change all Document Libraries in the site, you could try changing the Content Type: 如果您确实要更改站点中的所有文档库,则可以尝试更改内容类型:

using (SPSite site = new SPSite("http://localhost"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPContentType ct = web.ContentTypes[SPBuiltInContentTypeId.Document];
        SPField fld = web.Fields.GetField(fldName);
        SPFieldLink lnk = new SPFieldLink(fld);
        ct.FieldLinks.Add(lnk);
        ct.Update(true);
    }
}

The above code is shortened and modified from the example of SPContentType.Update Method (Boolean) . 上面的代码从SPContentType.Update方法(布尔)的示例中进行了简化和修改。 The MSDN article also has good general information on Updating Content Types . MSDN文章还提供了有关更新内容类型的良好常规信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM