Sharepoint 2010 - 使用服务器对象模型将标题、描述和关键字字段设为图片库中的必填字段

发布于 2024-12-13 10:31:03 字数 384 浏览 7 评论 0原文

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

I'm creating a Sharepoint feature, this feature has an event receiver associated to it. In the event receiver, I'm creating a Document Library and Picture Library using server-side object model. I'm also adding new custom columns (around 80) to these newly created document and picture library. Now I want to modify the properties of the Description, Keywords and Title fields that are by default created along with the picture library. I want to make these fields as Required fields. How do I do this? I tried to set SPList.AllowContentTypes = true and try to change the attributes of these fields, but it doesn't work (neither gives an error nor makes these required fields). I also tried to access the content types and try to change the attributes using SPContentType.FieldsLinks["Column_name"].Required and SPContentType.Fields["Column_name"].Required but it gives me an error. Does anyone have any other suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可是我不能没有你 2024-12-20 10:31:03

这是答案......

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

Here is the answer....

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();
简单爱 2024-12-20 10:31:03

您能告诉我们您在尝试使用字段链接时遇到的错误吗?因为这应该有效...我是这样做的:

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

can you tell us the error you got when trying to use the fieldlinks? Because this should work... I have done it like this:

SPContentType ct = web.Lists["*ListName*"].ContentTypes["*ContentTypeName*"];
SPFieldLinkCollection flinks = ct.FieldLinks;
flinks["*ColumnName*"].Required = true;
ct.update();
﹎☆浅夏丿初晴 2024-12-20 10:31:03

这应该可以解决问题:

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

This should do the trick:

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();
聊慰 2024-12-20 10:31:03

SPAllowContentTypes 不可设置。您可以尝试设置 ContentTypesEnabled

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

SPAllowContentTypes isn't settable. You might try setting ContentTypesEnabled instead.

I don't have a 2010 box to test against, but if SPAllowContentTypes returns false I think you're looking at modifying the definition of your picture library in the 14 hive (TEMPLATE\FEATURES\PictureLibrary\PicLib) to get what you're after. Tread lightly there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文