【问题标题】:SharePoint WSS3 - create document with a specific content typeSharePoint WSS3 - 创建具有特定内容类型的文档
【发布时间】:2011-09-09 07:15:15
【问题描述】:

我正在尝试在 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 事件都被触发。

【问题讨论】:

    标签: sharepoint content-type wss-3.0


    【解决方案1】:

    这对我有用。

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

    【讨论】:

    • 尝试解释为什么它有效以及为什么 OP 无效。
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 1970-01-01
      • 2013-03-26
      • 2019-12-28
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多