繁体   English   中英

SharePoint:内容类型ID与附加到列表实例的表单内容类型ID不同

[英]SharePoint: Content Type ID differs form content type id attached to a list instance

我遇到一个有关内容类型及其ID以及如何将其与对象模型一起使用的问题。

首先,我定义了一些网站列,使用这些列的内容类型以及通过CAML使用此内容类型的列表定义。 这三个组件中的每一个都作为一个功能实现。 另一个功能创建此列表定义的列表实例。

因此,当我尝试使用我的内容类型将新项目添加到列表时,我使用以下代码。

 SPListItem listItem = list.Items.Add();
 SPContentType type = list.ContentTypes[new ContentTypeId("0x010044fb4458c2eb4800825910845a35554c")];
 listItem["ContentTypeId"] = type.Id;
 listItem["Title"] = "Titel";
 listItem.Update();

当我执行这段代码时,类型对象仍然为null,同时我也确定内容类型已附加到列表中。 调试代码并检查列表的内容类型,可以看出附加到列表的内容类型没有在内容类型定义(CAML)中定义的ID。 列表实例中的id不同,但以我在内容类型定义中定义的ID开头。 0x010044FB4458C2EB4800825910845A35554C 0077D683BDD9969F4E920A27C334463321

那么这种行为正常吗? 我希望附加到列表的内容类型具有与定义中相同的ID。

我的主要目标是避免使用内容类型的名称从列表的内容类型中检索它,而是使用唯一的ID。

再见

当您基于内容类型创建列表时,列表的每个实例实际上都是从父级继承的新内容类型。

看看这个... http://soerennielsen.wordpress.com/2007/09/11/propagate-site-content-types-to-list-content-types/

更新-在不使用名称的情况下查找List实例的情况下,还请查看SPContentTypeUsage类!

不要过多地将内容类型的名称编码为代码中的常量。 乍一看似乎可以更改内容类型的名称,但应将其视为一个常量,因为更改内容类型名称的含义并不重要,以至于要求完全构建并重新发布您的解决方案,允许更改编码常量。

列表上的ContentTypeID是ContentTypeID +列表ID。

以下代码可以解决问题:

//i try and keep these in a string constants class to avoid 
//having to manage "magic strings" throughout my solution
SPContentTypeID ctid = new ContentTypeId("0x010044fb4458c2eb4800825910845a35554c");


//here's the magic
SPContentTypeID listCTID = list.ContentTypes.BestMatch(ctid);


//retrieve the ContentType with the appropriate ID
SPContentType type = list.ContentTypes[listCTID]; 


//do your thing    
SPListItem listItem = list.Items.Add(); 
listItem["ContentTypeId"] = type.Id; 
listItem["Title"] = "Titel"; 
listItem.Update();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM