SharePoint WSS3 - 创建具有特定内容类型的文档

发布于 2024-11-14 14:39:42 字数 650 浏览 6 评论 0原文

我正在尝试在 SharePoint (WSS 3.0) 文档库中创建具有特定内容类型的新文档。但新文档始终具有文档库的默认内容类型,而不是我指定的自定义内容类型:-(

我正在使用 SPFileCollection.Add 创建文件,并使用属性 Hashtable 来传递内容类型 ID/名称:

SPList list = web.Lists["Test Doc Lib"];

Hashtable properties = new Hashtable();
properties.Add("ContentType", "MyCustomContentType");
properties.Add("ContentTypeId", list.ContentTypes["MyCustomContentType"].Id.ToString());

list.RootFolder.Files.Add("Test File.html", Encoding.UTF8.GetBytes("test"), properties);

我能找到的所有示例代码都使用 SPFileCollection.Add 来初始创建文件,然后重新获取新的 SPListItem 并设置内容类型,我不想这样做,因为它会。导致两者要触发的 ItemAdding 和 ItemUpdating 事件。

I'm trying to create a new document in a SharePoint (WSS 3.0) document library with a specific content type. But the new document always has the default content type for the doc lib, not the custom one I specified :-(

I'm using SPFileCollection.Add to create the file, and using the properties Hashtable to pass the content type ID/name:

SPList list = web.Lists["Test Doc Lib"];

Hashtable properties = new Hashtable();
properties.Add("ContentType", "MyCustomContentType");
properties.Add("ContentTypeId", list.ContentTypes["MyCustomContentType"].Id.ToString());

list.RootFolder.Files.Add("Test File.html", Encoding.UTF8.GetBytes("test"), properties);

All the sample code I can find uses SPFileCollection.Add to initially create the file, then re-fetches the new SPListItem and sets the content type on this. I don't want to do this because it would cause both ItemAdding and ItemUpdating events to fire.

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

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

发布评论

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

评论(2

如果没有 2024-11-21 14:39:42

这对我有用。

SPContentTypeId id=list.ParentWeb.AvailableContentTypes["ContentTypeName"].Id;
Hashtable htMetaData = new Hashtable();
htMetaData.Add("ContentTypeId", id.ToString());
SPFile newfile = docSetFolder.Files.Add(fullFileUrl,fileByteArray,htMetaData,true);

This works for me.

SPContentTypeId id=list.ParentWeb.AvailableContentTypes["ContentTypeName"].Id;
Hashtable htMetaData = new Hashtable();
htMetaData.Add("ContentTypeId", id.ToString());
SPFile newfile = docSetFolder.Files.Add(fullFileUrl,fileByteArray,htMetaData,true);
枕花眠 2024-11-21 14:39:42

我从未尝试过以您的方式添加文件,但恕我直言,您可以创建文件,然后更改列表项内容类型。您可以禁用事件触发以防止 ItemAdding &触发 ItemUpdating 事件。有很多关于此的文章(搜索“sharepoint 禁用事件触发”)。一些链接:

http://blogs.syrinx.com/blogs/sharepoint/archive/2008/11/10/disabling-event-firing-on-splistitem-update- Differently.aspx

http://anylookup.com/how-to-really-disable-event-firing-in-sharepoint

I have never tried to add file in your way, but IMHO you can create file and then change list item content type. You can disable event firing to prevent ItemAdding & ItemUpdating events from firing. There're many articles about this (search for "sharepoint disable event firing"). Some links:

http://blogs.syrinx.com/blogs/sharepoint/archive/2008/11/10/disabling-event-firing-on-splistitem-update-differently.aspx

http://anylookup.com/how-to-really-disable-event-firing-in-sharepoint

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