Sharepoint 2010 - 使用server-object-model将标题,描述和关键字字段作为图片库中的必填字段

时间:2011-11-03 07:31:20

标签: sharepoint sharepoint-2010 sharepointfoundation2010 sharepoint-object-model

我正在创建一个Sharepoint功能,此功能有一个与之关联的事件接收器。在事件接收器中,我正在使用服务器端对象模型创建文档库和图片库。我还要为这些新创建的文档和图片库添加新的自定义列(大约80个)。现在我想修改默认情况下与图片库一起创建的Description,Keywords和Title字段的属性。我想将这些字段设为必填字段。我该怎么做呢?我试图设置SPList.AllowContentTypes = true并尝试更改这些字段的属性,但它不起作用(既不会出错也不会生成这些必填字段)。我还尝试访问内容类型并尝试使用SPContentType.FieldsLinks [“Column_name”]更改属性。必需和SPContentType.Fields [“Column_name”]。必需但它给我一个错误。有没有人有任何其他建议?

4 个答案:

答案 0 :(得分:2)

这是答案......

SPContentType ct = mypiclib.ContentTypes["Picture"];
SPFieldLinks titleLink = ct.FieldLinks["Title"];
SPFieldLinks descLink = ct.FieldLinks["comments"]; //internal name of Description
SPFieldLinks keywords = ct.FieldLinks["keywords"];
titlelink.Required = true;
descLink.Required = true;
keywords.Required = true;
ct.Update();

答案 1 :(得分:0)

你能告诉我们你在尝试使用fieldlinks时遇到的错误吗?因为这应该有用......我这样做了:

SPContentType ct = web.Lists["*ListName*"].ContentTypes["*ContentTypeName*"];
SPFieldLinkCollection flinks = ct.FieldLinks;
flinks["*ColumnName*"].Required = true;
ct.update();

答案 2 :(得分:0)

这应该可以解决问题:

SPWeb yourWeb =  ... //assign your web
SPList yourPictureLibrary = ... //assign your picture library

web.AllowUnsafeUpdates = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Title].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Description].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Keywords].Required = true;
yourPictureLibrary.Update();

答案 3 :(得分:0)

SPAllowContentTypes无法设置。您可以尝试设置ContentTypesEnabled

我没有2010年的盒子可供测试,但如果SPAllowContentTypes返回false,我认为您正在考虑修改14个配置单元中图片库的定义(TEMPLATE \ FEATURES \ PictureLibrary \ PicLib得到你想要的东西。轻轻地踩在那里。