R – SharePoint: Apply source ContentType to destination when copying a SPListItem

copymosssharepointsplistitem

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.

Best Answer

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...

Related Topic