Search code examples
sharepointmosscopysplistitem

SharePoint: Apply source ContentType to destination when copying a SPListItem


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.


Solution

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