views:

245

answers:

2

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.

A: 

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

Steve
A: 

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.

Steve