SharePoint:复制 SPListItem 时将源 ContentType 应用于目标

发布于 2024-08-18 03:52:46 字数 541 浏览 7 评论 0原文

您好,

我已经编写了自己的 ContentType 定义,可以应用于任何文档库。当用户将某个项目复制到我的 ContentType 定义尚未附加到的文档库时,我希望通过我的 ContentType 保留与该项目关联的数据。

显然要做的事情似乎是捕获新项目的 ItemAdding 或 ItemUpdating 事件,查看源项目以查看我的 ContentType 是否与其关联,然后在之前将我的 ContentType 添加到目标文档库副本实际发生。

问题是,我在这些事件中找不到任何信息来告诉我源项目是什么。唯一的此类数据位于最终的 ItemUpdated 事件中,但那时为时已晚......该项目已被复制,并且与我的 ContentType 关联的数据已被丢弃。

有人对我如何获得我想要的行为有任何想法吗?

TIA 寻求帮助!

Steve

PS:我想我能做的一件事是在 ItemUpdated 事件中获取源 Url,然后编写代码来添加 ContentType,并手动将源中与该类型关联的数据移动到目标。与我上面提出的解决方案相比,这似乎非常不优雅。

Greetings,

I've written my own ContentType definition that can be applied to any Document Library. I would like to preserve the data associated with an item via my ContentType when that item is copied by the user to a Document Library to which my ContentType definition is not yet attached.

The obvious thing to do, it seems, is to catch either the ItemAdding or ItemUpdating event for the new item, look at the source item to see if my ContentType is associated with it, and then add my ContentType to the destination Document Library prior to the copy actually happening.

The problem is, I can find no information in these events that tells me what the source item is. The only such data is in the final ItemUpdated event, but by then it is too late...the item has already been copied and the data associated with my ContentType discarded.

Anyone have any ideas as to how I can get the behavior I want?

TIA for any help!

Steve

PS: The one thing I guess I can do is get the source Url in the ItemUpdated event, and then write code to add the ContentType and also manually move the data associated with that type in the source to the destination. This just seems very inelegant compared to the solution I propose above.

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

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

发布评论

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

评论(2

清眉祭 2024-08-25 03:52:46

我想通了!答案是源 URL 位于 ItemUpdating 事件属性的 AfterProperties 字段中。你可以这样理解:

properties.AfterProperties["_CopySource"];

我已经查看了这个字段,但由于该对象中有一个成员在调试器中显示“Count = 0”,所以我感到困惑。我误读了这意味着里面什么都没有,但事实证明这个计数与里面有多少财产无关。哎呀。

不幸的是,我试图做的事情仍然不起作用。即使我在 ItemUpdating 事件中将 ContentType 添加到目标库,与该类型关联的字段也不会被复制。那好吧...

I figured it out! The answer is that the Source URL is in the AfterProperties field in the ItemUpdating event properties. You get at it like this:

properties.AfterProperties["_CopySource"];

I had looked at this field, but was thrown by the fact that there is a member in that object that shows "Count = 0" in the debugger. I misread that to mean that there was nothing in there, but it turns out that that count has nothing to do with how many properties are in there. Ooops.

Unfortunately, what I was trying to do still doesn't work. Even if I add my ContentType to the destination library in the ItemUpdating event, the fields associated with that type don't get copied across. Oh well...

恋你朝朝暮暮 2024-08-25 03:52:46

只是后续行动,以防有人对我的问题的最终解决方案感兴趣...

我做了什么来获得我想要的行为:

  1. 将我的代码移至 ItemUpdated 事件处理程序,以便我在复制已完全发生。

  2. 获取源项目 URL(请参阅前面的答案)并将其解析为源 SPList 和 SPListItem

  3. 查看查看我的 ContentType 是否在源中但不在目标中。

  4. 目标中,请在目标中安装 ContentType 并手动将该 ContentType 中的属性(通过查询 ContentType 本身)从源复制到目标。

此过程仅发生在复制到目标库的第一个项目上。之后,ContentType 已存在于目标库中,SP 本身会将该类型的字段值从源复制到目标。

Just a follow-up, in case someone is interested in the ultimate solution to my problem...

What I did to get the behavior I wanted:

  1. Moved my code to the ItemUpdated event handler so that I'm operating after the copy has completely occured.

  2. Get the source item URL (see prior answer) and resolve that to the source SPList and SPListItem

  3. Look to see if my ContentType is in the source but not the destination.

  4. If the ContentType is not in the destination, install the ContentType in the destination AND manually copy the properties in that ContentType (by querying the ContentType itself) from the source to the destination.

This process only happens for the first item that gets copied to the destination lib. After that, the ContentType already exists on the dest lib and SP itself copies the field values for that type from source to destination.

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