SharePoint WSS3 - 创建具有特定内容类型的文档
我正在尝试在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用。
This works for me.
我从未尝试过以您的方式添加文件,但恕我直言,您可以创建文件,然后更改列表项内容类型。您可以禁用事件触发以防止 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